-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCombinationSum.java
82 lines (63 loc) · 2.29 KB
/
CombinationSum.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
package org.sean.backtracking;
import java.util.*;
/***
* 39. Combination Sum
*/
public class CombinationSum {
List<Set<List<Integer>>> caches = new ArrayList<>(); // dp[]
// Solution 1 :
public List<List<Integer>> combinationSum0(int[] candidates, int target) {
if (candidates == null || candidates.length == 0 || target == 0)
return Collections.emptyList();
caches = new ArrayList<>(target + 1);
caches.add(Collections.emptySet());
int length = candidates.length;
for (int i = 1; i <= target; i++) {
Set<List<Integer>> dp = new HashSet<>();
for (int j = 0; j < length; j++) {
int candidate = candidates[j];
if (i == candidate) {
dp.add(new ArrayList<>(Arrays.asList(i)));
} else if (i > candidate) { // >
reduceSet(dp, candidate, i - candidate);
}
}
caches.add(dp);
}
Set<List<Integer>> result = caches.get(target);
return new LinkedList<>(result);
}
private void reduceSet(Set<List<Integer>> dpOut, int candidate, int rest) {
if (rest > 0) { // skip <= 0
Set<List<Integer>> sets = caches.get(rest);
if (sets != null) {
for (List<Integer> set : sets) {
List<Integer> matchedOut = new ArrayList<>(set);
matchedOut.add(candidate);
// !!n*lg(n)
Collections.sort(matchedOut);
dpOut.add(matchedOut);
}
}
}
}
// Solution 2 : DFS
public List<List<Integer>> combinationSum(int[] candidates, int target) {
search(candidates, target, 0, new LinkedList<>());
return out;
}
private List<List<Integer>> out = new ArrayList<>();
private void search(int[] candidates, int target, int pos, LinkedList<Integer> path) {
if (target < 0)
return;
if (target == 0) {
out.add(new ArrayList<>(path));
return;
}
for (int i = pos; i < candidates.length; i++) {
path.add(candidates[i]);
search(candidates, target - candidates[i], i, path);
path.removeLast();
}
}
}