Problem
Section titled “Problem”The set [1, 2, 3, ..., n] contains a total of n! unique permutations.
By listing and labeling all of the permutations in order, we get the following sequence for n = 3:
"123""132""213""231""312""321"
Given n and k, return the permutation sequence.
Example 1:
Input: n = 3, k = 3Output: "213"Example 2:
Input: n = 4, k = 9Output: "2314"Example 3:
Input: n = 3, k = 1Output: "123"Constraints:
Solution approach
Section titled “Solution approach”Intuition
Section titled “Intuition”Generating permutations one by one until the k-th costs up to . We can jump straight to the answer instead.
In lexicographic order the permutations are grouped by their first digit. With n digits available, each choice of first digit is followed by permutations of the rest. So the first permutations start with the smallest digit, the next with the second smallest, and so on. Using a 0-indexed rank k - 1, the first digit is the one at position among the remaining digits, and the rank inside that group is .
Remove that digit and repeat the same reasoning for the second position with , and so on. This is just writing k - 1 in the factorial number system. Switching to 0-indexed k is what makes the division exact; with 1-indexed k the last permutation of each group would land in the next group.
Algorithm
Section titled “Algorithm”- Fill
fact[i] = i!forifrom0ton - 1(at most , fits inint). - Put the digits
1..nindigits, in increasing order, and setk = k - 1. - For
ifromn - 1down to0:idx = k / fact[i]picks the digit; appenddigits[idx]toresultand erase it fromdigits.k %= fact[i]is the rank among permutations of the remaining digits.
- Return
result.
Walkthrough
Section titled “Walkthrough”Example 2: n = 4, k = 9, so the 0-indexed rank is 8.
i | fact[i] | k | idx = k / fact[i] | digits before | chosen | k % fact[i] |
|---|---|---|---|---|---|---|
| 3 | 6 | 8 | 1 | 1 2 3 4 | 2 | 2 |
| 2 | 2 | 2 | 1 | 1 3 4 | 3 | 0 |
| 1 | 1 | 0 | 0 | 1 4 | 1 | 0 |
| 0 | 1 | 0 | 0 | 4 | 4 | 0 |
The result is "2314".
Complexity
Section titled “Complexity”- Time: —
nsteps, each erasing from a list of at mostndigits. With this is effectively constant. - Space: —
fact,digitsand the result.
Edge cases
Section titled “Edge cases”k = 1(Example 3): the rank is0, everyidxis0, giving the digits in increasing order.k = n!: the rank isn! - 1, everyidxis the last remaining digit, giving the digits in decreasing order.n = 1: one step withfact[0] = 1returns"1".
Think before you peek
Work out an approach on your own first — the explanation will still be here when you want it.
class Solution { public: string getPermutation(int n, int k) { vector<int> fact(n, 1); for (int i = 1; i < n; i++) fact[i] = fact[i - 1] * i;
vector<char> digits; for (int d = 1; d <= n; d++) digits.push_back('0' + d);
string result; k--; for (int i = n - 1; i >= 0; i--) { int idx = k / fact[i]; result += digits[idx]; digits.erase(digits.begin() + idx); k %= fact[i]; } return result; }};class Solution { public String getPermutation(int n, int k) { int[] fact = new int[n]; fact[0] = 1; for (int i = 1; i < n; i++) fact[i] = fact[i - 1] * i;
List<Character> digits = new ArrayList<>(); for (int d = 1; d <= n; d++) digits.add((char) ('0' + d));
StringBuilder result = new StringBuilder(); k--; for (int i = n - 1; i >= 0; i--) { int idx = k / fact[i]; result.append(digits.remove(idx)); k %= fact[i]; } return result.toString(); }}class Solution: def getPermutation(self, n: int, k: int) -> str: fact = [1] * n for i in range(1, n): fact[i] = fact[i - 1] * i
digits = [str(d) for d in range(1, n + 1)]
result = [] k -= 1 for i in range(n - 1, -1, -1): idx, k = divmod(k, fact[i]) result.append(digits.pop(idx)) return ''.join(result)Give it a try first
Attempt the problem on LeetCode — the code will still be here.