The mathematical foundation of permutations tells us that a set of n distinct elements produces exactly n! unique permutations. When we arrange these permutations in lexicographical (dictionary) order, we create a predictable sequence.
For example, with n = 3, the set [1, 2, 3] generates these 6 permutations in order:
- 1st: "123"
- 2nd: "132"
- 3rd: "213"
- 4th: "231"
- 5th: "312"
- 6th: "321"
Your challenge: Given n and k, find the k-th permutation in this sequence without generating all previous permutations.
This problem tests your ability to think mathematically about combinatorics and avoid the computational explosion of factorial complexity.
Input & Output
Visualization
Time & Space Complexity
We iterate through n positions, and for each position we may need O(n) time to remove elements from the list
Space for storing the available digits list and result string
Constraints
- 1 ≤ n ≤ 9
- 1 ≤ k ≤ n!
- k is guaranteed to be valid (within the range of possible permutations)