-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathCombinationSumII.java
79 lines (66 loc) · 2.46 KB
/
CombinationSumII.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
package problems.leetcode;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Deque;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
// https://leetcode.com/problems/combination-sum-ii/
public class CombinationSumII {
public static void main(String[] args) {
for (List<Integer> l : combinationSum2(new int[] { 10, 1, 2, 2, 2, 2, 7, 6, 1, 1, 5 }, 8)) {
System.out.println(l);
}
// for (List<Integer> l : combinationSum2(new int[] { 10, 1, 2, 2, 7, 6, 1, 1, 5
// }, 8)) {
// System.out.println(l);
// }
}
// 1, 1, 1, 2, 2, 5, 6, 7, 10 target = 8
public static List<List<Integer>> combinationSum2(int[] candidates, int target) {
// container to hold the final combinations
List<List<Integer>> results = new ArrayList<>();
Deque<Integer> comb = new ArrayDeque<>();
Map<Integer, Integer> counter = new HashMap<>();
for (int candidate : candidates) {
counter.compute(candidate, (k, v) -> v != null ? v + 1 : 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 static void backtrack(Deque<Integer> comb,
int target, int i,
List<int[]> counter,
List<List<Integer>> results) {
if (target == 0) {
// make a deep copy of the current combination.
results.add(new ArrayList<Integer>(comb));
return;
} else if (target < 0) {
return;
}
for (int j = i; j < counter.size(); j++) {
int[] entry = counter.get(j);
int candidate = entry[0], freq = entry[1];
if (freq <= 0) {
continue;
}
// add a new element to the current combination
comb.addLast(candidate);
entry[1]--;
// continue the exploration with the updated combination
backtrack(comb, target - candidate, j, counter, results);
// backtrack the changes, so that we can try another candidate
entry[1]++;
comb.removeLast();
}
}
}