forked from Priyanshukeshri/Hacktoberfest-2k22
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path60. Permutation Sequence.cpp
More file actions
119 lines (106 loc) · 3.31 KB
/
60. Permutation Sequence.cpp
File metadata and controls
119 lines (106 loc) · 3.31 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
//backtracking
//TLE
//126 / 200 test cases passed.
class Solution {
public:
string ans;
void backtrack(int n, int k, string& s, vector<bool>& used, int& seq){
if(s.size() == n){
// cout << s << ", " << seq << endl;
if(seq == k){
ans = s;
}
++seq;
}else{
for(int i = 0; i < n; ++i){
if(!used[i]){
s += (i+1+'0');
used[i] = true;
backtrack(n, k, s, used, seq);
s.pop_back();
used[i] = false;
}
if(ans != "") break;
}
}
}
string getPermutation(int n, int k) {
string s = "";
vector<bool> used(n, false);
int seq = 1;
backtrack(n, k, s, used, seq);
return ans;
}
};
//backtracking + math
//Runtime: 4 ms, faster than 48.53% of C++ online submissions for Permutation Sequence.
//Memory Usage: 5.9 MB, less than 83.89% of C++ online submissions for Permutation Sequence.
class Solution {
public:
string ans;
vector<int> facts;
void backtrack(int n, int k, string& s, vector<bool>& used, int& seq){
if(s.size() == n){
// cout << s << ", " << seq << endl;
if(seq == k){
ans = s;
}
++seq;
}else if(seq + facts[n-s.size()-1] < k){
//skip the permutations formed by the remaining n-s.size() chars
seq += facts[n-s.size()-1];
}else{
for(int i = 0; i < n; ++i){
if(!used[i]){
s += (i+1+'0');
used[i] = true;
backtrack(n, k, s, used, seq);
s.pop_back();
used[i] = false;
}
if(ans != "") break;
}
}
}
string getPermutation(int n, int k) {
string s = "";
vector<bool> used(n, false);
int seq = 1;
facts = vector<int>(n);
facts[0] = 1;
for(int i = 2; i <= n; ++i){
facts[i-1] = facts[i-2] * i;
}
backtrack(n, k, s, used, seq);
return ans;
}
};
//math
//https://leetcode.com/problems/permutation-sequence/discuss/22507/%22Explain-like-I'm-five%22-Java-Solution-in-O(n)
//Runtime: 0 ms, faster than 100.00% of C++ online submissions for Permutation Sequence.
//Memory Usage: 5.9 MB, less than 86.08% of C++ online submissions for Permutation Sequence.
//time: O(N)
class Solution {
public:
string getPermutation(int n, int k) {
string ans = "";
vector<int> nums(n);
iota(nums.begin(), nums.end(), 1);
int fact = 1;
for(int i = 1; i <= n; ++i){
fact *= i;
}
--k; //1-based -> 0-based
for(int i = 0; i < n; ++i){
//there could be (n-1)! permutations if we exclude one char
fact /= (n-i);
//the kth permutation belongs to what group
int groupid = k/fact;
ans += (nums[groupid] + '0');
nums.erase(nums.begin()+groupid);
//there are "groupid" groups before the visited group
k -= fact * groupid;
}
return ans;
}
};