-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPermutations.java
More file actions
362 lines (285 loc) · 9.21 KB
/
Permutations.java
File metadata and controls
362 lines (285 loc) · 9.21 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
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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
/*
Iterative
The basic idea is, to permute n numbers, we can add the nth number into the resulting List<List<Integer>> from the n-1 numbers,
in every possible position.
For example, if the input num[] is {1,2,3}: First, add 1 into the initial List<List<Integer>> (let's call it "answer").
Then, 2 can be added in front or after 1. So we have to copy the List in answer (it's just {1}), add 2 in position 0 of {1},
then copy the original {1} again, and add 2 in position 1. Now we have an answer of {{2,1},{1,2}}.
There are 2 lists in the current answer.
Then we have to add 3. first copy {2,1} and {1,2}, add 3 in position 0; then copy {2,1} and {1,2}, and add 3 into position 1,
then do the same thing for position 3. Finally we have 2*3=6 lists in answer, which is what we want.
*/
class Solution {
public List<List<Integer>> permute(int[] num) {
List<List<Integer>> ans = new ArrayList<List<Integer>>();
if (num.length ==0) return ans;
List<Integer> l0 = new ArrayList<Integer>();
l0.add(num[0]);
ans.add(l0);
for (int i = 1; i< num.length; ++i){
List<List<Integer>> new_ans = new ArrayList<List<Integer>>();
for (int j = 0; j<=i; ++j){
for (List<Integer> l : ans){
List<Integer> new_l = new ArrayList<Integer>(l);
new_l.add(j,num[i]);
new_ans.add(new_l);
}
}
ans = new_ans;
}
return ans;
}
}
// Backtracking
class Solution {
public List<List<Integer>> permute(int[] nums) {
List<List<Integer>> list = new ArrayList<>();
// Arrays.sort(nums); // not necessary
backtrack(list, new ArrayList<>(), nums);
return list;
}
private void backtrack(List<List<Integer>> list, List<Integer> tempList, int [] nums){
if(tempList.size() == nums.length){
list.add(new ArrayList<>(tempList));
} else{
for(int i = 0; i < nums.length; i++){
if(tempList.contains(nums[i])) continue; // element already exists, skip
tempList.add(nums[i]);
backtrack(list, tempList, nums);
tempList.remove(tempList.size() - 1);
}
}
}
}
// Bottom up approach
public class Solution {
public List<List<Integer>> permute(int[] nums) {
List<List<Integer>> permutations = new ArrayList<>();
if (nums.length == 0) {
return permutations;
}
collectPermutations(nums, 0, new ArrayList<>(), permutations);
return permutations;
}
private void collectPermutations(int[] nums, int start, List<Integer> permutation,
List<List<Integer>> permutations) {
if (permutation.size() == nums.length) {
permutations.add(permutation);
return;
}
for (int i = 0; i <= permutation.size(); i++) {
List<Integer> newPermutation = new ArrayList<>(permutation);
newPermutation.add(i, nums[start]);
collectPermutations(nums, start + 1, newPermutation, permutations);
}
}
}
//Code flow
/*
nums = 1,2,3
start = 0, permutation = []
i = 0, newPermutation = [1]
start = 1, permutation = [1]
i = 0, newPermutation = [2, 1]
start = 2, permutation = [2, 1]
i = 0, newPermutation = [3, 2, 1]
i = 1, newPermutation = [2, 3, 1]
i = 2, newPermutation = [2, 1, 3]
i = 1, newPermutation = [1, 2]
start = 2, permutation = [1, 2]
i = 0, newPermutation = [3, 1, 2]
i = 1, newPermutation = [1, 3, 2]
i = 2, newPermutation = [1, 2, 3]
*/
// Base case and build approach
public class Solution {
public List<List<Integer>> permute(int[] nums) {
return permute(Arrays.stream(nums).boxed().collect(Collectors.toList()));
}
private List<List<Integer>> permute(List<Integer> nums) {
List<List<Integer>> permutations = new ArrayList<>();
if (nums.size() == 0) {
return permutations;
}
if (nums.size() == 1) {
List<Integer> permutation = new ArrayList<>();
permutation.add(nums.get(0));
permutations.add(permutation);
return permutations;
}
List<List<Integer>> smallPermutations = permute(nums.subList(1, nums.size()));
int first = nums.get(0);
for(List<Integer> permutation : smallPermutations) {
for (int i = 0; i <= permutation.size(); i++) {
List<Integer> newPermutation = new ArrayList<>(permutation);
newPermutation.add(i, first);
permutations.add(newPermutation);
}
}
return permutations;
}
}
// Code flow
/*
nums = 1,2,3
smallPermutations(2, 3)
smallPermutations(3)
return [[3]]
first = 2
permutation = [3]
i = 0, newPermutation = [2, 3]
i = 1, newPermutation = [3, 2]
return [[2, 3], [3, 2]]
first = 1
permutation = [2, 3]
i = 0, newPermutation = [1, 2, 3]
i = 1, newPermutation = [2, 1, 3]
i = 2, newPermutation = [2, 3, 1]
permutation = [3, 2]
i = 0, newPermutation = [1, 3, 2]
i = 1, newPermutation = [3, 1, 2]
i = 2, newPermutation = [3, 2, 1]
*/
// Recursive Backtracking
/**
* Recursive Backtracking. In this solution passing the index of the nums that
* needs to be set in the current recursion.
*
* Time Complexity: O(N * N!). Number of permutations = P(N,N) = N!. Each
* permutation takes O(N) to construct
*
* T(n) = n*T(n-1) + O(n)
* T(n-1) = (n-1)*T(n-2) + O(n-1)
* ...
* T(2) = (2)*T(1) + O(2)
* T(1) = O(N) -> To convert the nums array to ArrayList.
*
* Above equations can be added together to get:
* T(n) = n + n*(n-1) + n*(n-1)*(n-2) + ... + (n....2) + (n....1) * n
* = P(n,1) + P(n,2) + P(n,3) + ... + P(n,n-1) + n*P(n,n)
* = (P(n,1) + ... + P(n,n)) + (n-1)*P(n,n)
* = Floor(e*n! - 1) + (n-1)*n!
* = O(N * N!)
*
* Space Complexity: O(N). Recursion stack.
*
* N = Length of input array.
*/
class Solution {
public List<List<Integer>> permute(int[] nums) {
List<List<Integer>> result = new ArrayList<>();
if (nums == null || nums.length == 0) {
return result;
}
permutationsHelper(result, nums, 0);
return result;
}
private void permutationsHelper(List<List<Integer>> result, int[] nums, int start) {
if (start == nums.length - 1) {
List<Integer> list = new ArrayList<>();
for (int n : nums) {
list.add(n);
}
result.add(list);
return;
}
for (int i = start; i < nums.length; i++) {
swap(nums, start, i);
permutationsHelper(result, nums, start + 1);
swap(nums, start, i);
}
}
private void swap(int[] nums, int x, int y) {
int t = nums[x];
nums[x] = nums[y];
nums[y] = t;
}
}
// Iterative Solution
/**
* Iterative Solution
*
* The idea is to add the nth number in every possible position of each
* permutation of the first n-1 numbers.
*
* Time Complexity: O(N * N!). Number of permutations = P(N,N) = N!. Each
* permutation takes O(N) to construct
*
* T(n) = (x=2->n) ∑ (x-1)!*x(x+1)/2
* = (x=1->n-1) ∑ (x)!*x(x-1)/2
* = O(N * N!)
*
* Space Complexity: O((N-1) * (N-1)!) = O(N * N!). All permutations of the first n-1 numbers.
*
* N = Length of input array.
*/
class Solution {
public List<List<Integer>> permute(int[] nums) {
List<List<Integer>> result = new ArrayList<>();
if (nums == null || nums.length == 0) {
return result;
}
result.add(Arrays.asList(nums[0]));
for (int i = 1; i < nums.length; i++) {
List<List<Integer>> newResult = new ArrayList<>();
for (List<Integer> cur : result) {
for (int j = 0; j <= i; j++) {
List<Integer> newCur = new ArrayList<>(cur);
newCur.add(j, nums[i]);
newResult.add(newCur);
}
}
result = newResult;
}
return result;
}
}
// Recursive Backtracking using visited array
/**
* Recursive Backtracking using visited array.
*
* Time Complexity: O(N * N!). Number of permutations = P(N,N) = N!. Each
* permutation takes O(N) to construct
*
* T(n) = n*T(n-1) + O(n)
* T(n-1) = (n-1)*T(n-2) + O(n)
* ...
* T(2) = (2)*T(1) + O(n)
* T(1) = O(n)
*
* Above equations can be added together to get:
* T(n) = n (1 + n + n*(n-1) + ... + (n....2) + (n....1))
* = n (P(n,0) + P(n,1) + P(n,1) + ... + P(n,n-1) + P(n,n))
* = n * Floor(e*n!)
* = O(N * N!)
*
* Space Complexity: O(N). Recursion stack + visited array
*
* N = Length of input array.
*/
class Solution {
public List<List<Integer>> permute(int[] nums) {
List<List<Integer>> result = new ArrayList<>();
if (nums == null) {
return result;
}
helper(result, new ArrayList<>(), nums, new boolean[nums.length]);
return result;
}
private void helper(List<List<Integer>> result, List<Integer> temp, int[] nums, boolean[] visited) {
if (temp.size() == nums.length) {
result.add(new ArrayList<>(temp));
return;
}
for (int i = 0; i < nums.length; i++) {
if (visited[i]) {
continue;
}
temp.add(nums[i]);
visited[i] = true;
helper(result, temp, nums, visited);
visited[i] = false;
temp.remove(temp.size() - 1);
}
}
}