We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 5b217b8 commit 5212b90Copy full SHA for 5212b90
Dynamic Programming/377_Combination_Sum_IV.java
@@ -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