diff --git a/java/0377-combination-sum-iv.java b/java/0377-combination-sum-iv.java index 5d81fb575..a491b3045 100644 --- a/java/0377-combination-sum-iv.java +++ b/java/0377-combination-sum-iv.java @@ -1,5 +1,4 @@ -class Solution { - +class Solution { /* Tabulation Method -------------------------- T = Target, N = nums.length @@ -48,4 +47,26 @@ private int helper(int[] nums, int t, HashMap memo){ 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 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); + } }