Count of Subset Sum
Explore how to count the total number of subsets from a given set that sum exactly to a target value. Understand the naive recursive approach and then master the optimization using dynamic programming with both top-down memoization and bottom-up tabulation methods. Learn to improve time complexity from exponential to polynomial and effectively manage space using 2D lookup tables.
Statement
Given a set of positive numbers nums and a value target_sum, count the total number of subsets of the given set whose sum is equal to the target_sum.
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 -
target_sum -
nums[i]