-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
0377-combination-sum-iv.java
72 lines (62 loc) · 1.84 KB
/
0377-combination-sum-iv.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
class Solution {
/* Tabulation Method
--------------------------
T = Target, N = nums.length
Time complexity: O(T⋅N)
Space complexity: O(T)
*/
public int combinationSum4(int[] nums, int target) {
int[] dp = new int[target+1];
dp[0] = 1;
for(int currSum = 1; currSum < dp.length; currSum++){
for(int no : nums){
if(currSum - no >= 0){
dp[currSum] += dp[currSum - no];
}
}
}
return dp[target];
}
/* Memoization Method
--------------------------
T = Target, N = nums.length
Time complexity: O(T⋅N)
Space complexity: O(T) + Recursive Stack
*/
public int combinationSum4(int[] nums, int target) {
HashMap<Integer, Integer> memo = new HashMap<>();
return helper(nums, target, memo);
}
private int helper(int[] nums, int t, HashMap<Integer, Integer> memo){
if(t == 0)
return 1;
if(t < 0)
return 0;
if(memo.containsKey(t))
return memo.get(t);
int count = 0;
for(int no : nums){
count += helper(nums, t - no);
}
memo.put(t, count);
return count;
}
/**
Simple brute force, count num of combinations
using Map ds
Time complexity: O(n)
Space complexity: O(n)
*/
public int combinationSum4(int[] nums, int target) {
Map<Integer, Integer> cache = new HashMap<>();
cache.put(0, 1);
for (int i = 1; i < target + 1; i++) {
cache.put(i, 0);
for (int n : nums) {
int temp = cache.containsKey(i - n) ? cache.get(i - n) : 0;
cache.put(i, cache.get(i) + temp);
}
}
return cache.get(target);
}
}