Skip to content

Commit 42c41ac

Browse files
authored
Update Combination Sum IV.java
1 parent 1f561e4 commit 42c41ac

File tree

1 file changed

+18
-21
lines changed

1 file changed

+18
-21
lines changed

Medium/Combination Sum IV.java

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,22 @@
11
class Solution {
2-
public int combinationSum4(int[] nums, int target) {
3-
Integer[] dp = new Integer[target + 1];
4-
return getCount(target, nums, dp);
5-
}
6-
7-
public int getCount(int target, int[] nums, Integer[] dp) {
8-
if (dp[target] != null) {
9-
return dp[target];
2+
public int combinationSum4(int[] nums, int target) {
3+
Integer[] dp = new Integer[target + 1];
4+
return helper(nums, target, dp);
105
}
11-
if (target == 0) {
12-
return 1;
6+
7+
private int helper(int[] nums, int target, Integer[] dp) {
8+
if (dp[target] != null) {
9+
return dp[target];
10+
}
11+
if (target == 0) {
12+
return 1;
13+
}
14+
int count = 0;
15+
for (int num : nums) {
16+
if (target >= num) {
17+
count += helper(nums, target - num, dp);
18+
}
19+
}
20+
return dp[target] = count;
1321
}
14-
if (target < 0) {
15-
return 0;
16-
}
17-
int total = 0;
18-
for (int num : nums) {
19-
if (target >= num) {
20-
total += getCount(target - num, nums, dp);
21-
}
22-
}
23-
return dp[target] = total;
24-
}
2522
}

0 commit comments

Comments
 (0)