Dynamic Programming21 sections · 916 units
Open in Course

K Subsets - key observation

Track partial sum

Build subsets one at a time. For the subset being built, track how much sum you've accumulated. When it reaches target, start a new subset.

The mask tells you which elements are used. The partial sum (mod target) tells you how close the current subset is to complete. observation: dp[mask]dp[\text{mask}] can store the current partial sum. When dp[mask]=0dp[\text{mask}] = 0, you've just completed a subset and are starting fresh.