200. Permutation Sequence

Given two integers n and k, return the k-th permutation sequence of the numbers [1, 2, 3, ..., n].

The permutations are arranged in lexicographic order (i.e., dictionary order).

You must solve the problem without generating all permutations explicitly.

Example 1:

Input: n = 3, k = 3

Output: "213"

Explanation: The permutations in order are:

  • 1. 123
  • 2. 132
  • 3. 213 ← K = 3
  • 4. 231
  • 5. 312
  • 6. 321

Example 2:

Input: n = 3, k = 5

Output: "312"

Explanation: K = 5 corresponds to the fifth permutation in lexicographic order.

Now Your Turn!

Pick the correct output for the given input

Input: n = 4, k = 9

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

  • 1 <= n <= 9
  • 1 <= k <= n! (i.e., k is always a valid permutation index)

Fun Facts

0
class Solution {
public:
string getPermutation(int n, int k) {
// Your code goes here
}
};
Test Case

Input:

K
N