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 <= 91 <= k <= n!Problem Overview: Given two integers n and k, return the kth permutation of the numbers from 1 to n when all permutations are ordered lexicographically. Generating all permutations quickly becomes infeasible because the total count is n!.
Approach 1: Recursive Backtracking (Traditional Approach) (Time: O(n! * n), Space: O(n))
This approach generates permutations the same way classic permutation problems are solved using recursion and backtracking. Maintain a boolean array or set to track used numbers, build permutations one digit at a time, and recursively explore the remaining choices. Every time a full permutation of length n is formed, decrement k. When k reaches zero, that permutation is the answer. The downside is obvious: you still explore permutations sequentially until the kth one appears. Since there are n! permutations and building each takes O(n), the total time complexity is O(n! * n). This method is useful for understanding how permutations are constructed but does not scale for larger n.
Approach 2: Factorial Number System and Decrement Method (Time: O(n^2), Space: O(n))
The key observation from math is that permutations follow a predictable block pattern. For example, when n = 4, each first digit repeats for 3! = 6 permutations before changing. This means you can directly compute which digit belongs in each position instead of generating all permutations.
First, build a list containing numbers 1..n and precompute factorial values. Convert k to zero‑based indexing by subtracting 1. For each position, divide k by the factorial of the remaining digits to determine which number to pick from the list. Append that number to the result and remove it from the list. Update k using the remainder and continue with the next position. Each selection narrows the search space by an entire factorial block, effectively skipping thousands of permutations at once.
The algorithm iterates n times, and removing an element from a list costs O(n), resulting in overall O(n^2) time with O(n) space. This method avoids generating permutations entirely and directly constructs the kth permutation using factorial indexing.
Recommended for interviews: Interviewers expect the factorial number system approach. It demonstrates understanding of permutation ordering and mathematical reasoning rather than brute-force generation. Showing the backtracking idea first proves you understand permutation generation, but the factorial-based solution is the optimized answer typically required for a hard problem.
This approach leverages the factorial number system, where the number of permutations starting with a particular digit can be determined by the factorial of (n-1). We adjust k for zero-based indexing and deduct permutations as we determine each digit sequentially.
This C solution involves precomputing factorials and maintaining an array of available numbers. We reduce k by dividing and modulo operations to select which number should appear next in the permutation sequence.
C++
Java
Python
C#
JavaScript
Time Complexity: O(n^2) due to potential shifts in the numbers array.
Space Complexity: O(n) for storing factorials and numbers.
This approach generates permutations using a traditional backtracking method, with the goal of finding the k-th permutation without necessarily generating all permutations explicitly.
This C solution uses a recursive backtracking approach to generate permutations, counting them until reaching the desired k-th permutation, which is stored for output.
C++
Java
Python
C#
JavaScript
Time Complexity: O(n!) for generating permutations.
Space Complexity: O(n) due to recursion and string storage.
| Approach | Complexity |
|---|---|
| Factorial Number System and Decrement Method | Time Complexity: O(n^2) due to potential shifts in the numbers array. Space Complexity: O(n) for storing factorials and numbers. |
| Recursive Backtracking (Traditional Approach) | Time Complexity: O(n!) for generating permutations. Space Complexity: O(n) due to recursion and string storage. |
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Recursive Backtracking (Traditional) | O(n! * n) | O(n) | When demonstrating how permutations are generated or when n is very small |
| Factorial Number System and Decrement Method | O(n^2) | O(n) | Best approach for interviews and large n since it directly computes the kth permutation |
L18. K-th Permutation Sequence | Leetcode • take U forward • 231,609 views views
Watch 9 more video solutions →Practice Permutation Sequence with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor