9 lines DP Java solution.


  • 0
    O
    public static int combinationSum4(int[] nums, int target) {
       int[] memo = new int[target+1];
       Arrays.sort(nums);
       for(int i = 1; i <= target; i++) 
          for(int j = 0; j < nums.length; j++) {
             if (i < nums[j]) break;
        	 if(nums[j] == i) memo[i] += 1;
        	 else memo[i] += memo[i-nums[j]];
          }
        return memo[target];
       }
    

  • 0
    Y
    This post is deleted!

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.