-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathPermutation_Sequence.cpp
More file actions
26 lines (23 loc) · 891 Bytes
/
Permutation_Sequence.cpp
File metadata and controls
26 lines (23 loc) · 891 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
class Solution {
public:
void shift(string& permutation, int idx, int k) {
for(int i = idx + k - 1; i >= idx; --i) {
swap(permutation[i], permutation[i + 1]);
}
}
void getPermutationUtils(string& permutation, int idx, int k, vector<int>& fact) {
int n = (int) permutation.length();
if (k == 0 or idx == n) return;
shift(permutation, idx, k / fact[n - idx - 1]);
getPermutationUtils(permutation, idx + 1, k % fact[n - idx - 1], fact);
}
string getPermutation(int n, int k) {
string permutation = "";
vector<int> fact{1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880}; //n factorial
for (int i = 1; i <= n; i++) {
permutation += char(i + '0');
}
getPermutationUtils(permutation, 0, k - 1, fact);
return permutation;
}
};