|
| 1 | +--- |
| 2 | +id: permutation-sequence |
| 3 | +title: Permutation Sequence(LeetCode) |
| 4 | +sidebar_label: 0060-Permutation Sequence |
| 5 | +tags: |
| 6 | + - Math |
| 7 | + - Recursion |
| 8 | +description: The set [1, 2, 3, ..., n] contains a total of n! unique permutations. Given n and k, return the kth permutation sequence. |
| 9 | +--- |
| 10 | + |
| 11 | +## Problem Statement |
| 12 | + |
| 13 | +The set `[1, 2, 3, ..., n]` contains a total of `n!` unique permutations. |
| 14 | + |
| 15 | +By listing and labeling all of the permutations in order, we get the following sequence for `n = 3`: |
| 16 | + |
| 17 | +1. `"123"` |
| 18 | +2. `"132"` |
| 19 | +3. `"213"` |
| 20 | +4. `"231"` |
| 21 | +5. `"312"` |
| 22 | +6. `"321"` |
| 23 | +Given `n` and `k`, return the `kth` permutation sequence. |
| 24 | + |
| 25 | +### Examples |
| 26 | + |
| 27 | +**Example 1:** |
| 28 | + |
| 29 | +```plaintext |
| 30 | +Input: n = 3, k = 3 |
| 31 | +Output: "213" |
| 32 | +``` |
| 33 | + |
| 34 | +**Example 2:** |
| 35 | + |
| 36 | +```plaintext |
| 37 | +Input: n = 4, k = 9 |
| 38 | +Output: "2314" |
| 39 | +``` |
| 40 | + |
| 41 | +**Example 3:** |
| 42 | + |
| 43 | +```plaintext |
| 44 | +Input: n = 3, k = 1 |
| 45 | +Output: "123" |
| 46 | +``` |
| 47 | + |
| 48 | +### Constraints |
| 49 | + |
| 50 | +- `1 <= n <= 9` |
| 51 | +- `1 <= k <= n!` |
| 52 | + |
| 53 | +## Solution |
| 54 | + |
| 55 | +### Approach |
| 56 | + |
| 57 | +#### Algorithm |
| 58 | + |
| 59 | +1. Initialize Factorial Values: |
| 60 | +* Precompute and store the factorial values of integers from 0 to 9 in a vector `factVal` to get factorials in O(1) time. |
| 61 | +2. Initialize Array: |
| 62 | +* Create a vector `v` containing elements from 1 to `n`. |
| 63 | +3. Recursive Function `setPerm`: |
| 64 | +* Base Case: If `n == 1`, append the last remaining element in `v` to `ans` and return. |
| 65 | +* Calculate the required index using `k / factVal[n-1]`. |
| 66 | +* Handle the corner case where if `k` is a multiple of `(n-1)!`, decrement the index by 1. |
| 67 | +* Append the element at the calculated index from `v` to `ans`. |
| 68 | +* Remove the selected element from `v`. |
| 69 | +* Adjust the value of `k` to be the remainder after dividing by `factVal[n-1]`. |
| 70 | +* Make a recursive call with updated values of `n`, `k`, `v`, and `ans`. |
| 71 | +4. Construct Result: |
| 72 | +* Initialize an empty string `ans`. |
| 73 | +* Call the recursive function setPerm with initial values of `v`, `ans`, `n`, `k`, and `factVal`. |
| 74 | +* Return the result `ans`. |
| 75 | + |
| 76 | +#### Implementation |
| 77 | + |
| 78 | +```C++ |
| 79 | +class Solution { |
| 80 | +public: |
| 81 | + void setPerm(vector<int>& v, string& ans, int n, int k, vector<int>& factVal) { |
| 82 | + if (n == 1) { |
| 83 | + ans += to_string(v.back()); |
| 84 | + return; |
| 85 | + } |
| 86 | + |
| 87 | + int index = (k / factVal[n-1]); |
| 88 | + if (k % factVal[n-1] == 0) { |
| 89 | + index--; |
| 90 | + } |
| 91 | + |
| 92 | + ans += to_string(v[index]); |
| 93 | + v.erase(v.begin() + index); |
| 94 | + k -= factVal[n-1] * index; |
| 95 | + setPerm(v, ans, n-1, k, factVal); |
| 96 | + } |
| 97 | + |
| 98 | + string getPermutation(int n, int k) { |
| 99 | + if (n == 1) return "1"; |
| 100 | + |
| 101 | + vector<int> factVal = {1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880}; |
| 102 | + string ans = ""; |
| 103 | + vector<int> v; |
| 104 | + for (int i = 1; i <= n; i++) v.emplace_back(i); |
| 105 | + |
| 106 | + setPerm(v, ans, n, k, factVal); |
| 107 | + return ans; |
| 108 | + } |
| 109 | +}; |
| 110 | +``` |
| 111 | +
|
| 112 | +### Complexity Analysis |
| 113 | +
|
| 114 | +- **Time complexity**: $O(N^2)$ |
| 115 | +- **Space complexity**: $O(N)$ |
| 116 | +
|
| 117 | +### Conclusion |
| 118 | +
|
| 119 | +This algorithm efficiently finds the k-th permutation of a set of n elements by using a combination of precomputed factorials and recursive selection. The approach ensures that the computation is done in a systematic manner without generating all permutations. |
0 commit comments