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 k-th permutation sequence.
Example 1
Input: n = 4, k = 9
Output: 2314
Explanation: There are 3! = 6 permutations starting with each first digit. Permutations 1-6 start with 1 and 7-12 start with 2, so the 9th starts with 2 and is the 3rd of that group: 2134, 2143, 2314. The answer is "2314".
Example 2
Input: n = 3, k = 3
Output: 213
Explanation: In order, the permutations of 1-3 are 123, 132, 213, … so the 3rd is "213".
Example 3
Input: n = 3, k = 1
Output: 123
Explanation: The 1st permutation is the digits in increasing order: "123".
Fixing the first digit partitions the permutations into blocks of (n-1)! each. Integer-divide (k-1) by (n-1)! to pick which unused number leads, then recurse on the remainder.
Run checks all cases above. Submit evaluates all test cases.