Show problems as
Classic problem #46MediumArrayBacktracking

Permutations

Real-world version: Calibration Run Orders
Tracked in this browser only
Read Permutations on LeetCode(opens in a new tab)

The statement here is our own: the same inputs, outputs and technique.

A calibration bench applies a set of signed offsets, given as offsets, to an instrument. Each offset is applied exactly once per run, and the instrument drifts differently depending on which offset comes first, so the lab wants to inspect every run order before choosing one.

Return every arrangement of offsets that uses each offset exactly once, so n offsets yield n! arrangements. Each arrangement lists the offsets in the sequence they would be applied. The arrangements themselves may be handed back in whatever sequence you like.

The offsets are pairwise distinct, so no two arrangements are identical.

Examples

Example 1

Input
offsets = [-10, 0, 10]
Output
[[-10, 0, 10], [-10, 10, 0], [0, -10, 10], [0, 10, -10], [10, -10, 0], [10, 0, -10]]

Three offsets admit six run orders, one for each choice of which offset is applied first paired with each ordering of the remaining two.

Example 2

Input
offsets = [1, 2]
Output
[[1, 2], [2, 1]]

With two offsets the bench can only apply them in one order or the other.

Example 3

Input
offsets = [5]
Output
[[5]]

A single offset leaves nothing to reorder, so exactly one run order exists.

Constraints

  • 1 <= offsets.length <= 6
  • -10 <= offsets[i] <= 10
  • All of the offsets are distinct

The values you return may be in any order.

The signature

The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.

Python
def run_orders(offsets: list[int]) -> list[list[int]]:
Java
public List<List<Integer>> runOrders(int[] offsets)