-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathsubsets2.java
364 lines (314 loc) · 11.1 KB
/
subsets2.java
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
362
363
364
package LeetCodeJava.BackTrack;
// https://leetcode.com/problems/subsets-ii/
/**
* 90. Subsets II
* Solved
* Medium
* Topics
* Companies
* Given an integer array nums that may contain duplicates, return all possible subsets (the power set).
*
* The solution set must not contain duplicate subsets. Return the solution in any order.
*
*
*
* Example 1:
*
* Input: nums = [1,2,2]
* Output: [[],[1],[1,2],[1,2,2],[2],[2,2]]
* Example 2:
*
* Input: nums = [0]
* Output: [[],[0]]
*
*
* Constraints:
*
* 1 <= nums.length <= 10
* -10 <= nums[i] <= 10
*
*
*/
import java.util.*;
public class subsets2 {
// V0
// IDEA : BACKTRACK
// https://leetcode.com/problems/subsets/solutions/27281/a-general-approach-to-backtracking-questions-in-java-subsets-permutations-combination-sum-palindrome-partitioning/
public List<List<Integer>> subsetsWithDup(int[] nums) {
List<List<Integer>> list = new ArrayList<>();
/**
* NOTE !!!
*
* in order to skip duplicates via `compare cur and prev val`,
* we need to sort array first
*/
Arrays.sort(nums);
backtrack(list, new ArrayList<>(), nums, 0);
return list;
}
private void backtrack(List<List<Integer>> list, List<Integer> tempList, int [] nums, int start){
list.add(new ArrayList<>(tempList));
for(int i = start; i < nums.length; i++){
// skip duplicates
/**
* NOTE !!!
*
* below is the key shows how to simply skip duplicates
* (instead of using hashmap counter)
*/
if(i > start && nums[i] == nums[i-1]){
continue;
}
tempList.add(nums[i]);
backtrack(list, tempList, nums, i + 1);
tempList.remove(tempList.size() - 1);
}
}
// V0-1
// IDEA : Backtracking
// https://leetcode.com/problems/subsets-ii/editorial/
public List<List<Integer>> subsetsWithDup_0_1(int[] nums) {
/**
* NOTE !!!
*
* in order to skip duplicates via `compare cur and prev val`,
* we need to sort array first
*/
Arrays.sort(nums);
List<List<Integer>> subsets = new ArrayList<>();
List<Integer> currentSubset = new ArrayList<>();
_helper(subsets, currentSubset, nums, 0);
return subsets;
}
private void _helper(List<List<Integer>> subsets, List<Integer> currentSubset, int[] nums, int index) {
// Add the subset formed so far to the subsets list.
subsets.add(new ArrayList<>(currentSubset));
for (int i = index; i < nums.length; i++) {
// If the current element is a duplicate, ignore.
/**
* NOTE !!!
*
* below is the key shows how to simply skip duplicates
* (instead of using hashmap counter)
*/
if (i != index && nums[i] == nums[i - 1]) {
continue;
}
currentSubset.add(nums[i]);
subsetsWithDupHelper(subsets, currentSubset, nums, i + 1);
currentSubset.remove(currentSubset.size() - 1);
}
}
// V0-2
public List<List<Integer>> subsetsWithDup_0_2(int[] nums) {
List<List<Integer>> res = new ArrayList<>();
if (nums.length == 0) {
return res;
}
// Sort the array to handle duplicates effectively
Arrays.sort(nums);
// Backtrack
List<Integer> cur = new ArrayList<>();
this.getSubSet(0, nums, cur, res);
return res;
}
public void getSubSet(int start_idx, int[] nums, List<Integer> cur, List<List<Integer>> res) {
// Add current subset to the result list (deep copy to avoid reference issues)
res.add(new ArrayList<>(cur));
// Backtracking loop
for (int i = start_idx; i < nums.length; i++) {
// Skip duplicates: only include the number if it is the first occurrence at
// this level
if (i > start_idx && nums[i] == nums[i - 1]) {
continue;
}
cur.add(nums[i]);
// Recursively call to explore the next element, moving start index forward
this.getSubSet(i + 1, nums, cur, res);
// Backtrack (remove the last element added)
cur.remove(cur.size() - 1);
}
}
// // V0'
// // IDEA : BACKTRACK + LC 70
// List<List<Integer>> output = new ArrayList();
// int n, k;
//
// public void backtrack(int first, ArrayList<Integer> curr, int[] nums) {
// // if the combination is done
// if (curr.size() == k) {
// Collections.sort(curr);
// if(!output.contains(curr)){
// output.add(new ArrayList<>(curr));
// }
// return;
// }
//
// /** NOTE HERE !!!
// *
// * ++i : i+1 first, then do op
// * i++ : do op first, then i+1
// *
// * -> i++ or ++i is both OK here
// */
// for (int i = first; i < n; i++) {
// // add i into the current combination
// curr.add(nums[i]);
// // use next integers to complete the combination
// backtrack(i + 1, curr, nums);
// // backtrack
// curr.remove(curr.size() - 1);
// }
// }
//
// public List<List<Integer>> subsetsWithDup(int[] nums) {
// n = nums.length;
// /** NOTE HERE !!!
// *
// * ++k : k+1 first, then do op
// * k++ : do op first, then k+1
// *
// * -> k++ or ++k is both OK here
// */
// for (k = 0; k < n + 1; k++) {
// backtrack(0, new ArrayList<Integer>(), nums);
// }
// return output;
// }
// V1-1
// https://neetcode.io/problems/subsets-ii
// IDEA: BRUTE FOECE
Set<List<Integer>> res = new HashSet<>();
public List<List<Integer>> subsetsWithDup_1_1(int[] nums) {
Arrays.sort(nums);
backtrack(nums, 0, new ArrayList<>());
return new ArrayList<>(res);
}
private void backtrack(int[] nums, int i, List<Integer> subset) {
if (i == nums.length) {
res.add(new ArrayList<>(subset));
return;
}
subset.add(nums[i]);
backtrack(nums, i + 1, subset);
subset.remove(subset.size() - 1);
backtrack(nums, i + 1, subset);
}
// V1-2
// https://neetcode.io/problems/subsets-ii
// IDEA: BACKTRACK I
List<List<Integer>> res_1_2 = new ArrayList<>();
public List<List<Integer>> subsetsWithDup_1_2(int[] nums) {
Arrays.sort(nums);
backtrack(0, new ArrayList<>(), nums);
return res_1_2;
}
private void backtrack(int i, List<Integer> subset, int[] nums) {
if (i == nums.length) {
res_1_2.add(new ArrayList<>(subset));
return;
}
subset.add(nums[i]);
backtrack(i + 1, subset, nums);
subset.remove(subset.size() - 1);
while (i + 1 < nums.length && nums[i] == nums[i + 1]) {
i++;
}
backtrack(i + 1, subset, nums);
}
// V1-3
// https://neetcode.io/problems/subsets-ii
// IDEA: BACKTRACK II
List<List<Integer>> res_1_3 = new ArrayList<>();
public List<List<Integer>> subsetsWithDup_1_3(int[] nums) {
Arrays.sort(nums);
backtrack_1_3(0, new ArrayList<>(), nums);
return res_1_3;
}
private void backtrack_1_3(int i, List<Integer> subset, int[] nums) {
res_1_3.add(new ArrayList<>(subset));
for (int j = i; j < nums.length; j++) {
if (j > i && nums[j] == nums[j - 1]) {
continue;
}
subset.add(nums[j]);
backtrack_1_3(j + 1, subset, nums);
subset.remove(subset.size() - 1);
}
}
// V2
// IDEA : Bitmasking
// https://leetcode.com/problems/subsets-ii/editorial/
public List<List<Integer>> subsetsWithDup_2(int[] nums) {
List<List<Integer>> subsets = new ArrayList();
int n = nums.length;
// Sort the generated subset. This will help to identify duplicates.
Arrays.sort(nums);
int maxNumberOfSubsets = (int) Math.pow(2, n);
// To store the previously seen sets.
Set<String> seen = new HashSet<>();
for (int subsetIndex = 0; subsetIndex < maxNumberOfSubsets; subsetIndex++) {
// Append subset corresponding to that bitmask.
List<Integer> currentSubset = new ArrayList();
StringBuilder hashcode = new StringBuilder();
for (int j = 0; j < n; j++) {
// Generate the bitmask
int mask = 1 << j;
int isSet = mask & subsetIndex;
if (isSet != 0) {
currentSubset.add(nums[j]);
// Generate the hashcode by creating a comma separated string of numbers in the currentSubset.
hashcode.append(nums[j]).append(",");
}
}
if (!seen.contains(hashcode.toString())) {
seen.add(hashcode.toString());
subsets.add(currentSubset);
}
}
return subsets;
}
// V3
// IDEA : Cascading (Iterative)
// https://leetcode.com/problems/subsets-ii/editorial/
public List<List<Integer>> subsetsWithDup_3(int[] nums) {
Arrays.sort(nums);
List<List<Integer>> subsets = new ArrayList<>();
subsets.add(new ArrayList<Integer>());
int subsetSize = 0;
for (int i = 0; i < nums.length; i++) {
int startingIndex = (i >= 1 && nums[i] == nums[i - 1]) ? subsetSize : 0;
// subsetSize refers to the size of the subset in the previous step. This value also indicates the starting index of the subsets generated in this step.
subsetSize = subsets.size();
for (int j = startingIndex; j < subsetSize; j++) {
List<Integer> currentSubset = new ArrayList<>(subsets.get(j));
currentSubset.add(nums[i]);
subsets.add(currentSubset);
}
}
return subsets;
}
// V4
// IDEA : Backtracking
// https://leetcode.com/problems/subsets-ii/editorial/
public List<List<Integer>> subsetsWithDup_4(int[] nums) {
Arrays.sort(nums);
List<List<Integer>> subsets = new ArrayList<>();
List<Integer> currentSubset = new ArrayList<>();
subsetsWithDupHelper(subsets, currentSubset, nums, 0);
return subsets;
}
private void subsetsWithDupHelper(List<List<Integer>> subsets, List<Integer> currentSubset, int[] nums, int index) {
// Add the subset formed so far to the subsets list.
subsets.add(new ArrayList<>(currentSubset));
for (int i = index; i < nums.length; i++) {
// If the current element is a duplicate, ignore.
if (i != index && nums[i] == nums[i - 1]) {
continue;
}
currentSubset.add(nums[i]);
subsetsWithDupHelper(subsets, currentSubset, nums, i + 1);
currentSubset.remove(currentSubset.size() - 1);
}
}
}