Permutation Sequence
Permutation Sequence is LeetCode 60 (Hard). Write the numbers 1 to n in every possible order, sort those orderings like words in a dictionary, and number them from 1. Given n and k, return the ordering with number k, as a string of digits.
nis at most 9, so the digits are single characters and the largest count,9! = 362,880, fits in an int.kis always valid: between 1 andn!.
This page finds one ordering directly. Listing all of them is the job of Permutations.
- 1 <= n <= 9
- 1 <= k <= n!
Intuition
In dictionary order, the orderings come in blocks by their first digit. With 4 digits, all orderings starting with 1 come first, then all starting with 2, and so on, and each block holds 3! = 6 orderings, one for every way to arrange the other three digits.
So the first digit is just which block k lands in: divide by the block size. The position inside that block is the remainder, and it ranks an ordering of the remaining digits, which have the same block structure one size smaller. Repeat until no digits are left. Nothing is ever generated; each digit costs one division.
You are asked for one item at a known position in a sorted list of arrangements, and the list is far too long to build. When the list splits into equal-sized groups by its first choice, the position can be decoded by division instead of enumeration.
Approach
Before reading on: with digits 1 to 4, how many orderings start with 1? Using that, which digit must the 9th ordering start with? Aim for O(n²) time without generating any ordering.
Count from zero
Subtract 1 from k. With 0-based ranks, block b of size f covers ranks b·f to b·f + f - 1, so the block is exactly k // f. With 1-based k the last ordering of each block would divide into the next block.
Precompute the block sizes
Store fact[i] = i! for i from 0 to n - 1. When r digits remain, each of them leads a block of (r - 1)! orderings, so the loop reads fact from the largest entry down to 0! = 1.
Pick a digit, keep the remainder
Keep the unused digits in a sorted list. For each position, idx = k // fact[r - 1] is the block, so the digit is the idx-th unused one. Then k %= fact[r - 1] is the rank inside that block, which is the next problem to solve.
Remove the chosen digit
Delete it from the list so the next position only sees the digits still free. The list stays sorted after a deletion, which matters because block order follows digit order.
Why the blocks are equal
Fixing the first digit leaves r - 1 distinct digits, and every arrangement of them appears exactly once after it. Every first digit therefore gets (r - 1)! orderings, and dictionary order puts them side by side, smallest digit first. That equal size is the only fact the division relies on.
Code in Python, C++ and Java
Common pitfalls
Dividing the 1-based k
idx = k // fact[r - 1] # k not decremented
k -= 1 ... idx = k // fact[r - 1]
For n = 3, k = 2 the answer is "132". Without the decrement, 2 // 2 = 1 picks digit 2 first and returns "213". The last ordering of every block has a 1-based rank that is a multiple of the block size, so it always spills into the next block.
Picking the digit by value, not by position in the unused list
out.append(str(idx + 1))
out.append(digits.pop(idx))
idx counts blocks among the digits that are still free. Once 2 is used, block 1 belongs to digit 3, not 2. For n = 3, k = 4 the value version returns "221" instead of "231".
Edge cases
k = 1Every division gives 0, so each step takes the smallest free digit and the answer is 1 2 … n in order.
k = n!After the decrement k = n! - 1, every division picks the largest free digit, giving n … 2 1.
n = 1One pass with block size 0! = 1 picks the only digit, "1".
Complexity
n positions, and each removes one digit from a list of at most n, which shifts the rest. With n ≤ 9 that is at most 45 shifts. Space is the digit list, the factorials and the answer.Permutation Sequence FAQ
What is the factorial number system?
It writes a number with place values 0!, 1!, 2!, … instead of powers of ten, where the digit in the i! place is at most i. Writing k - 1 this way gives the Lehmer code of the kth ordering: each digit says how many smaller free digits were skipped at that position. The loop above produces those digits one by one with // and %.
How do you go the other way and find the rank of a given permutation?
Reverse the loop. For each position, count the free digits smaller than the one placed there, multiply that count by the block size for that position, and add it up. Add 1 at the end for a 1-based rank. For "231": digit 1 is free and smaller than 2, giving 1 × 2! = 2; then 1 is still free and smaller than 3, giving 1 × 1! = 1. The total is 3, so "231" is ordering number 4.
Why not reach the kth permutation by calling next permutation k - 1 times?
It is correct but costs O(k · n), and k can be 9! = 362,880. The block division reaches the answer in n steps however large k is. Next Permutation is the right tool when you need the orderings one after another, not one far ahead.