Knapsack / Subset DP
Track which capacities, sums, or resource combinations are reachable as items are considered.
Items have weights, values, counts, or a target sum, and each item may be chosen under constraints.
How to approach it
Write the state and transition first. For 0/1 items with 1D compression, iterate capacity downward so the same item is not reused.
Loop direction changes whether items can be reused. Large numeric targets may make pseudo-polynomial DP impractical.
Practice question
Can [1, 4, 6, 9] be split into two groups with equal sums?
values = [1, 4, 6, 9], total = 20Give your answer before revealing the explanation.Show a hint
If the total is even, look for a subset summing to half the total.
Show an answer
Yes. One group is [1, 9] and the other is [4, 6].
Knapsack / Subset DP at work
A team has a $10,000 budget and project costs [6,000, 5,000, 4,000]. What is the most money it can spend without exceeding the budget?
Show one possible approach
$10,000, selecting the $6,000 and $4,000 projects. A subset-sum DP tracks which totals are reachable as each project is considered.
Practice next
Use these problem IDs to find additional practice for this pattern: