-
Notifications
You must be signed in to change notification settings - Fork 0
/
(Hard)Permutation Sequence.cpp
48 lines (46 loc) · 1.07 KB
/
(Hard)Permutation Sequence.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
40
41
42
43
44
45
46
47
48
// unoptimised (TLE)
// Test cases cleared
class Solution {
public:
set<string> dict;
void make_permutations(string in, string out)
{
if(in.length()==1)
{
dict.insert(out+in);
return;
}
for(int i=0;i<in.length();i++)
{
make_permutations(in.substr(1),out+in[0]);
rotate(in.begin(), in.begin()+1,in.end());
}
}
string getPermutation(int n, int k) {
string s = "";
for(int i=1;i<=n;i++)
{
string it = to_string(i);
s += it;
}
//cout<<s;
string op = "";
make_permutations(s,op);
set<string>::iterator it;
//for (it = dict.begin(); it!=dict.end(); ++it)
//{
// cout<<it<<endl;
//}
int i = 1;
for (it = dict.begin(); it!=dict.end(); ++it)
{ if(i>=k){
break;
}
dict.erase(it);
i++;
}
auto mn = dict.begin();
string ans = *mn;
return ans;
}
};