Suppose we have a list of distinct numbers called nums and another number k, we have to find the number of distinct combinations that sum up to k. You can reuse numbers when creating combinations.
So, if the input is like nums = [2, 4, 5] k = 4, then the output will be 2, as we can make two such groups like [2, 2] and .
To solve this, we will follow these steps:
Let us see the following implementation to get better understanding:
class Solution: def solve(self, nums, k): table =  +  * k for num in nums: for i in range(num, k + 1): table[i] += table[i - num] return table[k] ob = Solution() nums = [2, 4, 5] k = 4 print(ob.solve(nums, k))
[2, 4, 5], 4