805. K-th Permutation Sequence

HardRecursionRecursion

Given n and k (1-indexed), return the k-th permutation of [1,2,...,n] in lex order as a quoted string. Use factorial number system to directly compute each digit without generating all permutations.

Input: Two integers n and k.

Output: Quoted string.

Examples

Example 1
Input: 3, 3
Output: "213"
Explanation: Permutations of [1,2,3]: 123,132,213,...; 3rd is 213.
Example 2
Input: 4, 9
Output: "2314"
Explanation: 9th permutation of [1,2,3,4].

Constraints

Asked by

AmazonGoogleMicrosoftMetaAdobe
Solve this problem in the editor →