-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path056.1-Next-Permutation.cpp
332 lines (241 loc) · 7.06 KB
/
056.1-Next-Permutation.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
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define wt(x) int x; cin>>x; while( x-- )
// find next permutation of given arr
// YT - https://www.youtube.com/watch?v=LuLCLgMElus&list=TLPQMDgwOTIwMjIhMyJo4RzDxQ&index=3&ab_channel=takeUforward
class Solution1 {
public:
// void Next_permutation( vector<int> &arr ){
// int x=-1,y;
// for( int i=0; i<arr.size()-1; i++ ) // Traverse from front
// if( arr[i] < arr[i+1] )
// x=i;
// if( x != -1 ){
// for( int i=x+1; i<arr.size(); i++ ) // Traverse from front
// if( arr[i] > arr[x])
// y = i;
// swap( arr[x],arr[y] );
// reverse( arr.begin()+x+1, arr.end() );
// }
// else
// reverse(arr.begin(), arr.end() );
// }
void Next_permutation(vector<int>& arr) {
int x, y;
int n = arr.size();
for (x = n - 2; x >= 0; x--) // Traverse from back
if (arr[x] < arr[x + 1])
break;
if (x >= 0) {
for (y = n - 1; y >= x + 1; y--) // Traverse from back
if (arr[y] > arr[x])
break;
swap(arr[x], arr[y]);
reverse(arr.begin() + x + 1, arr.end());
} else // if( x < 0)
reverse(arr.begin(), arr.end());
}
};
// to find kth Permutation arr[ 1, 2, 3, ..., n ]
// Yt - https://youtu.be/W9SIlE2jhBQ
class Solution21 {
vector<int> fact, digits;
void solve(string& ans, int n, int k) {
if (n == 1) {
ans += to_string(digits.back());
return;
}
int indx = k / fact[n - 1];
if (k % fact[n - 1] == 0)
indx--;
ans += to_string(digits[indx]);
digits.erase(digits.begin() + indx);
// k = k - (size of blocks)*(number of blocks);
k -= fact[n - 1] * indx;
solve(ans, n - 1, k);
}
public:
string getPermutation(int n, int k) {
fact.push_back(1);;
for (int i = 1; i < n; i++)
fact.push_back(fact[i - 1] * i);
for (int i = 1; i <= n; i++)
digits.push_back(i);
string ans = "";
solve(ans, n, k);
return ans;
}
// n=3, k=1
// ans = "123"
};
// to find kth Permutation arr[ 1, 2, 3, ..., n ]// Best Approach
// YT - https://www.youtube.com/watch?v=wT7gcXLYoao&list=TLPQMDgwOTIwMjIhMyJo4RzDxQ&index=4&ab_channel=takeUforward
class Solution22 {
public:
string getPermutation(int n, int k) {
int fact = 1;
vector<int> nums;
for (int i = 1; i <= n - 1; i++) {
nums.push_back(i);
fact *= i;
}
nums.push_back(n);
k = k - 1; // due to 0 indexing
string ans = "";
while (true) {
ans = ans + to_string(nums[k / fact]);
nums.erase(nums.begin() + k / fact);
if (nums.size() == 0) break;
k = k % fact;
fact /= nums.size();
}
return ans;
}
// n = 4, k = 9 // "2314"
// n = 3, k = 3 // "213"
};
// find all permutation of a array and return it
class Solution31 {
vector<int> next_permu(vector<int>& num) {
int k, l, n = num.size();
for (k = n - 2; k >= 0; k--)
if (num[k] < num[k + 1])
break;
if (k < 0) {
reverse(num.begin(), num.end());
} else {
for (l = n - 1; l > k; l--)
if (num[l] > num[k])
break;
swap(num[k], num[l]);
reverse(num.begin() + k + 1, num.end());
}
return num;
}
public:
vector<vector<int>> permute(vector<int>& nums) {
int fact = 1, n = nums.size();
for (int i = 1; i <= n; i++)
fact *= i;
vector<vector<int>> permu;
if (n == 0) return permu;
permu.push_back(nums);
fact--;
while (fact--)
permu.push_back(next_permu(nums));
return permu;
}
};
// find all permutation of a array and return it // Better approach
// YT - https://www.youtube.com/watch?v=YK78FU5Ffjw&ab_channel=takeUforward
class Solution32 {
void solve(vector<int>& ds, vector<int>& nums, vector<vector<int>>& ans, int freq[]) {
if (ds.size() == nums.size()) {
ans.push_back(ds);
return;
}
for (int i = 0; i < nums.size(); i++) {
if (!freq[i]) {
freq[i] = 1;
ds.push_back(nums[i]);
solve(ds, nums, ans, freq);
freq[i] = 0;
ds.pop_back();
}
}
}
public:
vector<vector<int>> permute(vector<int>& nums) {
int n = nums.size();
vector<vector<int>> ans;
vector<int> ds;
int freq[n];
memset(freq, 0, sizeof(freq));
solve(ds, nums, ans, freq);
return ans;
}
};
// find all permutation of a array and return it // Best approach
// YT - https://www.youtube.com/watch?v=f2ic2Rsc9pU&list=TLPQMDgwOTIwMjIhMyJo4RzDxQ&index=2&ab_channel=takeUforward
class Solution33 {
void solve(int ind, vector<int>& nums, vector<vector<int>>& ans) {
if (ind == nums.size()) {
ans.push_back(nums);
return;
}
for (int i = ind; i < nums.size(); i++) {
swap(nums[ind], nums[i]);
solve(ind + 1, nums, ans);
swap(nums[ind], nums[i]);
}
}
public:
vector<vector<int>> permute(vector<int>& nums) {
vector<vector<int>> ans;
solve(0, nums, ans);
return ans;
}
};
// find all permutation of a string and return it // chars can be duplicate // duplicates permutation not allowed // must size(str) <= 9
class Solution34 {
public:
// Store all unique permutation of array in in set st
void permute(char* in, int i, set<string>& st) {
if (!in[i]) {
// cout<<in<<sp;
st.insert(in);
return;
}
// rec case
for (int j = i; in[j] != '\0'; j++) {
swap(in[i], in[j]);
permute(in, i + 1, st);
// Backtracking
swap(in[j], in[i]);
}
}
};
int main() {
Solution1 sol1;
int n; cin >> n;
vector<int> arr(n);
for (auto& i : arr) cin >> i;
cout << "\n";
int number_of_permutation = 5; // let say 5
while (number_of_permutation--) {
sol1.Next_permutation(arr);
for (auto i : arr)
cout << i << " ";
cout << "\n";
// if( next_permutation(arr.begin(),arr.end() ) ) {
// for( auto i:arr )
// cout<<i<<" ";
// cout<<"\n";
// }
// else {
// cout<<"Previous permutation is Greatest permutation \n";
// break;
// }
}
return 0;
}
/*
9
5 1 7 6 3 9 8 4 2
5 1 7 6 4 2 3 8 9
5 1 7 6 4 2 3 9 8
5 1 7 6 4 2 8 3 9
5 1 7 6 4 2 8 9 3
5 1 7 6 4 2 9 3 8
*/
/*
5
5 4 3 2 1
1 2 3 4 5
1 2 3 5 4
1 2 4 3 5
1 2 4 5 3
1 2 5 3 4
//Previous permutation is Greatest permutation
*/