Count of Subset Sum
Explore how to count the number of subsets from a set that sum up to a target value using dynamic programming. Learn to optimize naive recursive methods with top-down memoization and bottom-up tabulation approaches in the 0/1 Knapsack context, improving time and space efficiency.
Statement
Given a set of positive numbers nums and a value targetSum, count the total number of subsets of the given set whose sum is equal to the targetSum.
Let’s say you are given a set = and a target sum = . The output will be 2 as the following subsets:
- 4
will add up to make the desired sum.
Constraints:
-
nums.length -
targetSum -
nums[i]