Dynamic Programming21 sections · 916 units
Open in Course

Pattern - Knapsack DP

The framework

All knapsack problems follow this structure:

1.1. Items with attributes (weight, value, count limit)

2.2. Capacity constraint (total weight, budget, time)

3.3. Improvement goal (max value, min cost, count ways) Choose your formulation based on constraints: small WW means standard dp[i][w]dp[i][w], large WW with small values means value-based dp[i][v]dp[i][v], items repeat means unbounded dp[w]dp[w].

Recognize the pattern, pick the right formulation, write the transition. That's the knapsack framework.