Problem 556041 · easy · Phase 05 Advanced Algorithms & Graphs

Every Running Order

backtracking · permutations · recursion

A relay team must decide the order in which its runners go. The runners are identified by distinct bib numbers in the list bibs. Return a list containing every possible running order, each order being a list of all the bib numbers. The orders may appear in any sequence.

Examples

Input:  bibs = [7, 2, 9]
Output: [[7, 2, 9], [7, 9, 2], [2, 7, 9], [2, 9, 7], [9, 7, 2], [9, 2, 7]]

Input:  bibs = [4]
Output: [[4]]

Input:  bibs = []
Output: [[]]
Explanation: there is exactly one (empty) order for no runners.

Constraints

  • 0 <= len(bibs) <= 6, all bib numbers distinct
  • The result has len(bibs)! orders (720 at most).

Goals

  • Build permutations by choosing one unused element per level
  • Undo a choice after the recursive call returns (un-choose)
Starting Python…