-
Notifications
You must be signed in to change notification settings - Fork 125
/
Copy pathdonghyeon95.java
60 lines (53 loc) · 1.73 KB
/
donghyeon95.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
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.stream.Collectors;
class Solution {
private HashMap<Integer, List<String>> dp = new HashMap<>();
private HashSet<Integer> set;
public List<List<Integer>> combinationSum(int[] candidates, int target) {
set = new HashSet<>(Arrays.stream(candidates).boxed().toList());
recurse(target);
// Convert dp entries back to List<List<Integer>> for return
return dp.getOrDefault(target, new ArrayList<>()).stream()
.map(str -> Arrays.stream(str.split(" "))
.map(Integer::valueOf)
.collect(Collectors.toList()))
.collect(Collectors.toList());
}
public void recurse(int target) {
if (dp.containsKey(target)) return;
List<String> combinations = new ArrayList<>();
for (int i = 1; i < target + 1; i++) {
if (set.contains(i)) {
int remaining = target - i;
recurse(remaining);
if (dp.containsKey(remaining)) {
for (String combination : dp.get(remaining)) {
List<Integer> newCombination = new ArrayList<>(Arrays.stream(combination.split(" "))
.map(Integer::valueOf)
.toList());
newCombination.add(i);
newCombination.sort(Comparator.reverseOrder());
String newCombinationStr = newCombination.stream()
.map(String::valueOf)
.collect(Collectors.joining(" "));
if (!combinations.contains(newCombinationStr)) {
combinations.add(newCombinationStr);
}
}
}
}
}
if (set.contains(target)) {
String singleCombination = String.valueOf(target);
if (!combinations.contains(singleCombination)) {
combinations.add(singleCombination);
}
}
dp.put(target, combinations);
}
}