Permutations

Medium

This is an extra practice problem. It doesn't have walkthrough slides. You can solve it with the pattern template you already know. Open the hints if you get stuck.

Question

Given a list of distinct integers, build every possible arrangement of all the values in the list. Two arrangements count as different whenever the values appear in a different sequence.

Return every arrangement you find. You can list the arrangements in any order. The order of the values inside each arrangement is what makes it unique.

Input: nums = [1, 2]

Output: [[1, 2], [2, 1]]

With 2 values, there are 2 ways to line them up.

Input: nums = [4, 6, 9]

Output: [[4, 6, 9], [4, 9, 6], [6, 4, 9], [6, 9, 4], [9, 4, 6], [9, 6, 4]]

With 3 values, there are 6 ways to line them up: pick which value goes first, then which of the remaining two goes second.

Input: nums = []

Output: [[]]

An empty list has exactly one arrangement: the empty one.

Clarify the problem

What are some questions you'd ask an interviewer?

Understand the problem

How many arrangements should be returned for nums = [3, 6, 9]?
3
6
9
27

Remember to understand the problem and plan your approach before you start coding.