-
Notifications
You must be signed in to change notification settings - Fork 0
/
_060.cpp
39 lines (36 loc) · 871 Bytes
/
_060.cpp
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
class Solution {
public:
string getPermutation(int n, int k) {
int *save = new int[n];
int sum = 1;
bool *V = new bool[n];
save[0] = 1;
for(int i = 1;i < n;i++){
sum *= i;
save[i] = sum;
V[i] = true;
}
V[0] = true;
k--;
int i = n-1;
string ret = "";
while(i >= 0){
int index = k/save[i];
int num = find(V,n,index);
V[num] = false;
num++;
ret += to_string(num);
k %= save[i];
i--;
}
return ret;
}
int find(bool* V, int n,int index){
int count = -1;
for(int i = 0;i < n;i++){
if(V[i]) count++;
if(V[i] && count == index) return i;
}
return n-1;
}
};