Logo
IconChallenge
Icon
IconProblem
IconSolutions
IconSubmissions

Permutation Sequence

XPChallenge Points: 30
levelLevel: Hard

You are given a sequence of integers from 1 to n — that is, the set [1, 2, 3, …, n].
If all the permutations of this set are arranged in lexicographic order, your task is to return the k-th permutation in that order.

For example, when n = 3, all permutations in order are:

"123", "132", "213", "231", "312", "321"

Given the values of n and k, find and return the k-th string in this sequence.

Example 1:

Input: n = 3, k = 5

Output: "312"

Explanation:

The 5th permutation in the sequence [1,2,3] is "312".

Example 2:

Input: n = 4, k = 15

Output: "3214"

Explanation:

When all permutations of [1,2,3,4] are ordered, the 15th one is "3214".

Example 3:

Input: n = 2, k = 2

Output: "21"

Explanation:

The permutations are ["12", "21"]. The 2nd one is "21".

to Continue
like
dislike

Accepted:

Submission:

IconReport an issue
Icon
IconCode
IconYou need toto run or submitYou need toto run or submit
IconTest Case
IconTest Result