/60. Permutation Sequence

60. Permutation Sequence

Hard
Probability52.3% acceptance

Given two integers total_elements and sequence_index, return the sequence_index-th lexicographically ordered permutation of the integers from 1 to total_elements, as a string. The sequence_index is 1-based.

Example 1

Input: total_elements=5, sequence_index=10

Output: 14325

Explanation: The 10th permutation of [1,2,3,4,5] is 14325.

Example 2

Input: total_elements=4, sequence_index=15

Output: 3241

Explanation: The 15th permutation of [1,2,3,4] is 3241.

Constraints

  • 1 <= total_elements <= 9
  • 1 <= sequence_index <= factorial(total_elements)
Python (current runtime)

Case 1

Input: total_elements=3, sequence_index=5

Expected: 312

Case 2

Input: total_elements=6, sequence_index=20

Expected: 145326

Case 3

Input: total_elements=2, sequence_index=2

Expected: 21

Case 4

Input: total_elements=7, sequence_index=100

Expected: 1354276