Skip to content

Commit 5212b90

Browse files
Sean PrashadSean Prashad
authored andcommitted
Add 377_Combination_Sum_IV.java
1 parent 5b217b8 commit 5212b90

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
class Solution {
2+
public int combinationSum4(int[] nums, int target) {
3+
int[] dp = new int[target + 1];
4+
Arrays.fill(dp, -1);
5+
dp[0] = 1;
6+
7+
return helper(nums, target, dp);
8+
}
9+
10+
private int helper(int[] nums, int remainder, int[] dp) {
11+
if (remainder < 0) {
12+
return 0;
13+
}
14+
if (remainder == 0) {
15+
return 1;
16+
}
17+
18+
if (dp[remainder] != -1) {
19+
return dp[remainder];
20+
}
21+
22+
int result = 0;
23+
for (int i = 0; i < nums.length; i++) {
24+
result += helper(nums, remainder - nums[i], dp);
25+
}
26+
27+
dp[remainder] = result;
28+
return result;
29+
}
30+
}

0 commit comments

Comments
 (0)