DEV Community

Discussion on: Solution: Combination Sum IV

Collapse
 
rohithv07 profile image
Rohith V
class Solution {
    public int combinationSum4(int[] nums, int target) {
        if (nums.length == 1 && nums[0] != target)
            return 0;
        int [] dp = new int [target + 1];
        for (int i=1; i<=target; i++) {
            for (int number : nums) {
                if (i == number) {
                    dp[i] += 1;
                }
                else if (i - number > 0) {
                    dp[i] += dp[i - number];
                }
            }
        }
        return dp[target];
    }
}
Enter fullscreen mode Exit fullscreen mode

My Approach.

github.com/Rohithv07/LeetCodeTopIn...