1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| class Solution { public int combinationSum4(int[] nums, int target) { int[] dp = new int[target+1]; for(int num : nums){ if(num <= target){ dp[num] = 1; } } dp[0] = 1;
for(int i = 1; i <= target; i++){ int count = 0; for(int num : nums){ if(i - num >=0 ){ count += dp[i - num]; } } dp[i] = Math.max(dp[i], count); } return dp[target]; } }
|