k-th Lexicographic Permutation

HardMathCombinatoricsSortingBacktracking

Description

Return the k-th permutation (1-indexed) of [1, 2, ..., n] in lexicographic order, as a list.

Examples

Input:n = 3, k = 1
Output:[1,2,3]
Explanation:

The first permutation of [1,2,3] in lex order is [1,2,3].

Input:n = 3, k = 4
Output:[2,3,1]
Explanation:

Lex order is 123, 132, 213, 231, 312, 321; the 4th permutation is [2,3,1].

Input:n = 4, k = 24
Output:[4,3,2,1]
Explanation:

With n = 4 there are 24 permutations and the 24th in lex order is the reversed sequence [4,3,2,1].

Constraints

  • 1 ≤ n ≤ 10
  • 1 ≤ k ≤ n!

Ready to solve this problem?

Practice solo or challenge other developers in a real-time coding battle!