Every Seating Plan

Easy · Backtracking · about 15 min

Guests with distinct id numbers will sit in a row of chairs, one guest per chair. Return every possible seating plan (a plan lists the ids from the first chair to the last).

  • Return the plans sorted the way Python sorts lists (compare item by item).
  • guests holds 0 to 7 distinct integers, in any order, possibly negative.
  • With no guests there is exactly one plan, the empty one, so return [[]].

seat_orders([3, 1, 2]) → [[1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 1, 2], [3, 2, 1]]

Hints

  • Fill one chair at a time. Keep a list of which guests are already seated.
  • For the next chair, try each unseated guest: mark them, recurse, then unmark them.
  • Sort the guests first and try them in that order; the plans then come out sorted with no extra work.

Learn the pattern first: the Backtracking lesson.

More Backtracking problems