Math Fundamentals18 sections · 814 units
Open in Course

Problem - Permutation Sequence

(LeetCode 60)

Given n and k, return the k-th permutation of [1, 2, ..., n] in lexicographic order.

Example: n=3, k=3. Permutations in order: [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2,1]. The 3rd is [2,1,3].

Generating all permutations and picking the k-th works but is slow for large n. Can you compute the k-th directly?

Try it yourself first.