Combination Sum IV

Given an integer array with all positive numbers and no duplicates, find the number of possible combinations that add up to a positive integer target. Tags: Dynamic Programming

Try It!

Discussion

Video

Solution

class Solution:
    def combinationSum4(self, nums: List[int], target: int) -> int:
        dp = [0 for i in range(target + 1)]
        dp[0] = 1
        
        for i in range(1, target + 1):
            for j in range(len(nums)):
                num = nums[j]
                if num <= i:
                    dp[i] += dp[i - num]
        
        return dp[target]