Leetcode 377 Solution

This article provides solution to leetcode question 377 (combination-sum-iv)

https://leetcode.com/problems/combination-sum-iv

Solution

class Solution:
    def combinationSum4(self, nums: List[int], target: int) -> int:
        dp = [0] * (target + 1)

        nums.sort()
        dp[0] = 1
        for i in range(target + 1):
            for num in nums:
                if i - num >= 0:
                    dp[i] += dp[i - num]
                else:
                    break
        return dp[target]