When to Use Knapsack DP

Binary choices, additive limits

Use knapsack DP when you see these signals:

1.1. Binary choices per item. You include or exclude each element.

2.2. Additive constraints. Weights, counts, or sums that accumulate.

3.3. Optimization or counting. Maximize value, minimize cost, or count ways.

The state typically tracks your current index and remaining capacity. Time complexity is O(n×W)O(n \times W) where nn is items and WW is the constraint bound. Watch for when WW is large. You may need different techniques.

60105078-9885-4ff2-8017-6b178a047946.png