-
Notifications
You must be signed in to change notification settings - Fork 43
/
CombinationSum2.java
226 lines (198 loc) · 7.54 KB
/
CombinationSum2.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
package LeetCodeJava.BackTrack;
// https://leetcode.com/problems/combination-sum-ii/
import java.util.*;
public class CombinationSum2 {
// V0
// IDEA : BACKTRACK
// TODO : fix with hashmap avoid duplicates
// List<List<Integer>> ans = new ArrayList<>();
// public List<List<Integer>> combinationSum2(int[] candidates, int target) {
//
// if (candidates.length == 0 || Arrays.stream(candidates).sum() < target){
// //ans.add(new ArrayList<>());
// return ans;
// }
//
// Arrays.sort(candidates);
//
// HashMap<Integer, Integer> counter = new HashMap<>();
// for (int candidate : candidates) {
// if (counter.containsKey(candidate))
// counter.put(candidate, counter.get(candidate) + 1);
// else
// counter.put(candidate, 1);
// }
//
// ArrayList<Integer> cur = new ArrayList<>();
// int[] _candidates = candidates;
// _help(candidates, target, cur, 0, counter);
// return this.ans;
// }
//
// private void _help(int[] candidates, int target, ArrayList cur, int idx, HashMap counter){
//
// int _sum = _getSum(cur);
//
// if (cur.size() > candidates.length ||_sum > target){
// return;
// }
//
// if (_sum == target){
// Collections.sort(cur);
// if(!this.ans.contains(cur)){
// this.ans.add(new ArrayList<>(cur));
// return;
// }
// }
//
// for (int i = idx; i < candidates.length; i++){
// int val = candidates[i];
// if (val > target){
// return;
// }
// cur.add(val);
// int _cnt = (int) counter.get(val);
// counter.put(val, _cnt - 1);
// // recursive
// _help(candidates, target, cur, idx+1, counter);
// // undo
// counter.put(val, _cnt + 1);
// cur.remove(cur.size()-1);
// }
//
// }
//
// private int _getSum(ArrayList<Integer> cur){
// int res = 0;
// for(int x : cur){
// res += x;
// }
// return res;
// }
// V1
// IDEA : Backtracking with Counters
// https://leetcode.com/problems/combination-sum-ii/editorial/
public List<List<Integer>> combinationSum2_1(int[] candidates, int target) {
// container to hold the final combinations
List<List<Integer>> results = new ArrayList<>();
LinkedList<Integer> comb = new LinkedList<>();
HashMap<Integer, Integer> counter = new HashMap<>();
for (int candidate : candidates) {
if (counter.containsKey(candidate))
counter.put(candidate, counter.get(candidate) + 1);
else
counter.put(candidate, 1);
}
// convert the counter table to a list of (num, count) tuples
List<int[]> counterList = new ArrayList<>();
counter.forEach((key, value) -> {
counterList.add(new int[]{key, value});
});
backtrack(comb, target, 0, counterList, results);
return results;
}
private void backtrack(LinkedList<Integer> comb,
int remain, int curr,
List<int[]> counter,
List<List<Integer>> results) {
if (remain <= 0) {
if (remain == 0) {
// make a deep copy of the current combination.
results.add(new ArrayList<Integer>(comb));
}
return;
}
for (int nextCurr = curr; nextCurr < counter.size(); ++nextCurr) {
int[] entry = counter.get(nextCurr);
Integer candidate = entry[0], freq = entry[1];
if (freq <= 0)
continue;
// add a new element to the current combination
comb.addLast(candidate);
counter.set(nextCurr, new int[]{candidate, freq - 1});
// continue the exploration with the updated combination
backtrack(comb, remain - candidate, nextCurr, counter, results);
// backtrack the changes, so that we can try another candidate
counter.set(nextCurr, new int[]{candidate, freq});
comb.removeLast();
}
}
// V2
// IDEA : Backtracking with Index
// https://leetcode.com/problems/combination-sum-ii/editorial/
public List<List<Integer>> combinationSum2_2(int[] candidates, int target) {
List<List<Integer>> results = new ArrayList<>();
LinkedList<Integer> comb = new LinkedList<>();
Arrays.sort(candidates);
backtrack(candidates, comb, target, 0, results);
return results;
}
private void backtrack(int[] candidates, LinkedList<Integer> comb,
Integer remain, Integer curr,
List<List<Integer>> results) {
if (remain == 0) {
// copy the current combination to the final list.
results.add(new ArrayList<Integer>(comb));
return;
}
for (int nextCurr = curr; nextCurr < candidates.length; ++nextCurr) {
if (nextCurr > curr && candidates[nextCurr] == candidates[nextCurr - 1])
continue;
Integer pick = candidates[nextCurr];
// optimization: early stopping
if (remain - pick < 0)
break;
comb.addLast(pick);
backtrack(candidates, comb, remain - pick, nextCurr + 1, results);
comb.removeLast();
}
}
// V3
// 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>> combinationSum2_3(int[] nums, int target) {
List<List<Integer>> list = new ArrayList<>();
Arrays.sort(nums);
backtrack(list, new ArrayList<>(), nums, target, 0);
return list;
}
private void backtrack(List<List<Integer>> list, List<Integer> tempList, int [] nums, int remain, int start) {
if (remain < 0) return;
else if (remain == 0) list.add(new ArrayList<>(tempList));
else {
for (int i = start; i < nums.length; i++) {
if (i > start && nums[i] == nums[i - 1]) continue; // skip duplicates
tempList.add(nums[i]);
backtrack(list, tempList, nums, remain - nums[i], i + 1);
tempList.remove(tempList.size() - 1);
}
}
}
// V4
// https://www.youtube.com/watch?v=rSA3t6BDDwg
// https://github.com/neetcode-gh/leetcode/blob/main/java/0040-combination-sum-ii.java
public List<List<Integer>> combinationSum2_4(int[] candidates, int target) {
Arrays.sort(candidates);
List<List<Integer>> ans = new ArrayList<List<Integer>>();
List<Integer> ls = new ArrayList<Integer>();
comb(candidates, target, ans, ls, 0);
return ans;
}
public void comb(
int[] candidates,
int target,
List<List<Integer>> ans,
List<Integer> ls,
int index
) {
if (target == 0) {
ans.add(new ArrayList(ls));
} else if (target < 0) return; else {
for (int i = index; i < candidates.length; i++) {
if (i > index && candidates[i] == candidates[i - 1]) continue;
ls.add(candidates[i]);
comb(candidates, target - candidates[i], ans, ls, i + 1);
ls.remove(ls.get(ls.size() - 1));
}
}
}
}