-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path0060-permutation-sequence.cpp
More file actions
60 lines (52 loc) · 1.21 KB
/
0060-permutation-sequence.cpp
File metadata and controls
60 lines (52 loc) · 1.21 KB
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
class Solution {
private:
void solve(vector<int>& candidates, string& ans, int k) {
int n = candidates.size();
if (k < 1) {
for (int i = 0; i < n; ++i) {
ans += ('0' + candidates[i]);
}
return;
}
int x = 1, n_times = 1;
for (int i = 1; i <= n; ++i) {
if (n_times * i > k) {
x = i - 1;
break;
}
n_times *= i;
}
for (int i = 0; i < n - x - 1; ++i) {
ans += '0' + candidates[i];
}
ans += '0' + candidates[n - x - 1 + k / n_times];
candidates.erase(candidates.begin() + n - x - 1 + k / n_times);
candidates.erase(candidates.begin(), candidates.begin() + n - x - 1);
solve(candidates, ans, k % n_times);
}
public:
string getPermutation(int n, int k) {
vector<int> candidates;
for (int i = 1; i <= n; ++i) {
candidates.push_back(i);
}
string ans = "";
solve(candidates, ans, k - 1);
return ans;
}
};
/*
1234
1243
1324
1342
1423
1432
2134
2143
2314
2341
k=9 --
3*2*1=6
2
*/