Skip to content

Commit 0a2c2b2

Browse files
Merge pull request #2992 from benmak11/combination-sum-iv
Update 0377-combination-sum-iv
2 parents 0925768 + e61e593 commit 0a2c2b2

File tree

1 file changed

+23
-2
lines changed

1 file changed

+23
-2
lines changed

java/0377-combination-sum-iv.java

+23-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
class Solution {
2-
1+
class Solution {
32
/* Tabulation Method
43
--------------------------
54
T = Target, N = nums.length
@@ -48,4 +47,26 @@ private int helper(int[] nums, int t, HashMap<Integer, Integer> memo){
4847
memo.put(t, count);
4948
return count;
5049
}
50+
51+
/**
52+
Simple brute force, count num of combinations
53+
using Map ds
54+
Time complexity: O(n)
55+
Space complexity: O(n)
56+
*/
57+
public int combinationSum4(int[] nums, int target) {
58+
Map<Integer, Integer> cache = new HashMap<>();
59+
60+
cache.put(0, 1);
61+
62+
for (int i = 1; i < target + 1; i++) {
63+
cache.put(i, 0);
64+
for (int n : nums) {
65+
int temp = cache.containsKey(i - n) ? cache.get(i - n) : 0;
66+
cache.put(i, cache.get(i) + temp);
67+
}
68+
}
69+
70+
return cache.get(target);
71+
}
5172
}

0 commit comments

Comments
 (0)