Repository navigation
Expand file tree
/
Copy pathHowSum.java
More file actions
75 lines (65 loc) · 1.97 KB
/
Copy pathHowSum.java
File metadata and controls
75 lines (65 loc) · 1.97 KB
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
package DSA.Dynamic;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
// return null if you cant make targetSum
//combinatoric problem
public class HowSum {
public static void main(String[] args) {
System.out.println(howSum(7, new int[]{5, 3, 4, 7}));
}
private static List<Integer> howSum(int targetSum, int[] nums) {
if (targetSum == 0) {
return new ArrayList<>();
}
if (targetSum < 0) {
return null;
}
for (int num : nums) {
int newTargetSum = targetSum - num;
List<Integer> integerList = howSum(newTargetSum, nums);
if (integerList != null) {
List<Integer> sumList = new ArrayList<>(integerList);
sumList.add(num);
return sumList;
}
}
return null;
}
private static final Map<Integer, List<Integer>> memo = new HashMap<>();
private static List<Integer> howSumMemo(int targetSum, int[] nums) {
if (memo.containsKey(targetSum)) {
return memo.get(targetSum);
}
if (targetSum == 0) {
return new ArrayList<>();
}
if (targetSum < 0) {
return null;
}
for (int num : nums) {
int newTargetSum = targetSum - num;
List<Integer> integerList = howSumMemo(newTargetSum, nums);
if (integerList != null) {
List<Integer> sumList = new ArrayList<>(integerList);
sumList.add(num);
memo.put(newTargetSum, sumList);
return sumList;
}
}
memo.put(targetSum, null);
return null;
}
/*
complexity
if m is the target sum
depth will be -1(worst case) on each level. So depth = m
width is the number of elements in nums array n
so, n^m
space:
depth = n
recursion does not create new arrays. O(1)
total = O(n)
*/
}