60 Permutation Sequence

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 kth permutation sequence.

Example 1:

Input: n = 3, k = 3
Output: "213"

Example 2:

Input: n = 4, k = 9
Output: "2314"

Example 3:

Input: n = 3, k = 1
Output: "123"

Constraints:

  • 1 <= n <= 9
  • 1 <= k <= n!
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
class Solution:
    def getPermutation(self, n: int, k: int) -> str:
        elements = list(range(1, n+1))
        f = math.factorial(n)
        k -= 1
        res = ''
        while len(elements) > 0:
            f //= len(elements)
            i, k = k // f, k % f
            res += str(elements.pop(i))
        return res