-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKthPermutationSequence.cpp
59 lines (51 loc) · 1.01 KB
/
KthPermutationSequence.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
49
50
51
52
53
54
55
56
57
58
59
// Brute Force
// TC O(N!) SC O(N!)
void permute(string s, string ans, vector<string> &res)
{
if(s.size()==0)
{
res.push_back(ans);
return;
}
for(int i=0;i<s.size();i++)
{
ans+=s[i];
permute(s.substr(0,i)+s.substr(i+1),ans,res);
ans.pop_back();
}
}
string kthPermutation(int n, int k) {
vector<string> res;
string s="";
for(int i=1;i<=n;i++)
{
s+=to_string(i);
}
permute(s,"",res);
return res[k-1];
}
// Optimized approach
// TC O(N^2) SC O(N)
string kthPermutation(int n, int k) {
// Write your code here.
vector<int> number;
int fact=1;
for(int i=1;i<n;i++)
{
fact=fact*i;
number.push_back(i);
}
number.push_back(n);
string ans="";
k=k-1;
while(true)
{
ans=ans+to_string(number[k/fact]);
number.erase(number.begin()+k/fact);
if(number.size()==0)
break;
k=k%fact;
fact=fact/number.size();
}
return ans;
}