Skip to the workspace
Pebellslearn by building
Backtracking  ›  Lesson

Arrangements: Every Order With a Used List

Permutations 14 minTo list every ordering, fill one position at a time with an item not yet used, recurse, then remove it and mark it unused again, which produces n factorial orderings.

leetcode / Arrangements: Every Order With a Used List

Explore each scene at your own pace.

01 · The rule

The rule

Arrangements: Every Order With a Used List

A courier has parcels for three friends, Ana, Ben and Cy, and wants to compare every possible order of visits before choosing a route. Subsets asked "which items?"; this asks "in which order?" The tree changes shape, but the choose, explore, un-choose rhythm stays exactly the same.

The rule

Fill one slot at a time from what's unused, then hand it back.

Instead of deciding item by item, decide slot by slot: who is visited first, then second, then third. For each slot, try every person not already placed. A used list remembers who is placed, so nobody appears twice.

def orderings(items):
    result, path = [], []
    used = [False] * len(items)
    def explore():
        if len(path) == len(items):     # every slot filled
            result.append(path[:])
            return
        for i, item in enumerate(items):
            if used[i]:
                continue                # already placed in an earlier slot
            used[i] = True              # choose
            path.append(item)
            explore()
            path.pop()                  # un-choose: undo BOTH changes
            used[i] = False
    explore()
    return result

The first slot has n options, the second n − 1, and so on, so there are n × (n − 1) × … × 1 orderings: n factorial, written n!. That grows even faster than 2ⁿ: 10 items already give 3,628,800 orderings, so permutation problems come with very small limits.

Compare the two shapes side by side:

  • Subsets: one level per item, two branches each (take or leave), 2ⁿ leaves. Order does not matter.
  • Orderings: one level per slot, one branch per unused item, n! leaves. Order is the whole point.

One caution: if two items are identical, such as two parcels for the same address, this code lists some orderings twice. That calls for an extra skip rule, in the same spirit as skipping a repeated fixed value in the triples lesson.

Scene 1 of 5
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.