Skip to content

Commit 52cab23

Browse files
committed
Practise 21-Jul-2020
1 parent dcb3aa8 commit 52cab23

File tree

2 files changed

+132
-0
lines changed

2 files changed

+132
-0
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
The set [1,2,3,...,n] contains a total of n! unique permutations.
2+
3+
By listing and labeling all of the permutations in order, we get the following sequence for n = 3:
4+
5+
"123"
6+
"132"
7+
"213"
8+
"231"
9+
"312"
10+
"321"
11+
Given n and k, return the kth permutation sequence.
12+
13+
Note:
14+
15+
Given n will be between 1 and 9 inclusive.
16+
Given k will be between 1 and n! inclusive.
17+
Example 1:
18+
19+
Input: n = 3, k = 3
20+
Output: "213"
21+
Example 2:
22+
23+
Input: n = 4, k = 9
24+
Output: "2314"
25+
26+
27+
28+
29+
30+
31+
32+
33+
class Solution {
34+
public:
35+
vector<int> fact, digit;
36+
37+
void solve(int n, int k, string &res){
38+
if(n==1){
39+
res.push_back(digit[0]+'0');
40+
return;
41+
}
42+
43+
int index=k/fact[n-1];
44+
if((k%fact[n-1])==0) index-=1; // boundary case
45+
46+
res.push_back(digit[index]+'0'); // add digit to index
47+
digit.erase(digit.begin()+index); // remove used number
48+
k-=fact[n-1]*index;
49+
solve(n-1, k, res);
50+
}
51+
52+
string getPermutation(int n, int k) {
53+
string res;
54+
if(n==0) return res;
55+
56+
fact.push_back(1); // 0!=1
57+
for(int i=1;i<=n;i++)
58+
fact.push_back(fact[i-1]*i);
59+
60+
for(int i=1;i<=n;i++)
61+
digit.push_back(i);
62+
63+
solve(n, k, res);
64+
65+
return res;
66+
}
67+
};
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
2+
The set [1,2,3,...,n] contains a total of n! unique permutations.
3+
4+
By listing and labeling all of the permutations in order, we get the following sequence for n = 3:
5+
6+
"123"
7+
"132"
8+
"213"
9+
"231"
10+
"312"
11+
"321"
12+
Given n and k, return the kth permutation sequence.
13+
14+
Note:
15+
16+
Given n will be between 1 and 9 inclusive.
17+
Given k will be between 1 and n! inclusive.
18+
Example 1:
19+
20+
Input: n = 3, k = 3
21+
Output: "213"
22+
Example 2:
23+
24+
Input: n = 4, k = 9
25+
Output: "2314"
26+
27+
28+
29+
30+
31+
class Solution {
32+
public:
33+
vector<int> fact, digit;
34+
35+
void solve(int n, int k, string &res){
36+
if(n==1){
37+
res.push_back(digit[0]+'0');
38+
return;
39+
}
40+
41+
int index=k/fact[n-1];
42+
if((k%fact[n-1])==0) index-=1; // boundary case
43+
44+
res.push_back(digit[index]+'0'); // add digit to index
45+
digit.erase(digit.begin()+index); // remove used number
46+
k-=fact[n-1]*index;
47+
solve(n-1, k, res);
48+
}
49+
50+
string getPermutation(int n, int k) {
51+
string res;
52+
if(n==0) return res;
53+
54+
fact.push_back(1); // 0!=1
55+
for(int i=1;i<=n;i++)
56+
fact.push_back(fact[i-1]*i);
57+
58+
for(int i=1;i<=n;i++)
59+
digit.push_back(i);
60+
61+
solve(n, k, res);
62+
63+
return res;
64+
}
65+
};

0 commit comments

Comments
 (0)