0/1 Knapsack
Imagine you're packing a suitcase for a trip. You have a list of items — each with a weight and a value — and your bag can hold at most W kilograms. The catch: you either pack an item whole or leave it behind. You can't take half a jacket or a quarter of a laptop. That's the 0/1 Knapsack problem.
The "0/1" just means each item has two states: 0 (left behind) or 1 (packed). No fractions, no splitting.
Why Greedy Doesn't Work Here
Your instinct might be: take the item with the best value-per-kilo ratio first, then the next best, and so on. That's the greedy approach — but it fails here. Taking the best-ratio item might leave awkward remaining space that only fits cheap filler (contrast this with Fractional Knapsack, where splitting items eliminates wasted space). The items interact — and DP handles those interactions exactly.
The Big Idea: Include or Exclude
Define dp[i][w] = maximum value you can pack using the first i items and weight limit w. For each item, you face exactly two choices:
Leave item i out: dp[i][w] = dp[i-1][w] — same as best without this item.
Pack item i (only if it fits, i.e. wt[i] ≤ w): dp[i][w] = dp[i-1][w - wt[i]] + val[i].
You take whichever choice is larger. Base cases: dp[0][w] = 0 for all w (no items = no value), dp[i][0] = 0 for all i (zero capacity = nothing fits).
Space Optimization to 1D
Notice dp[i][w] only ever looks at the row above it (row i-1). So you can collapse the 2D table into a single 1D array. But here's the catch: you must iterate weight from W down to wt[i]. If you went left-to-right, you'd accidentally let item i contribute to itself — using it multiple times. Right-to-left keeps each item to exactly one use.
Reconstructing What You Packed
The final dp[n][W] tells you the maximum value. To find out which items you packed, trace back through the full 2D table from cell (n, W): if dp[i][w] differs from dp[i-1][w], item i was included — subtract its weight and move up. Otherwise, item i was skipped — just move up. Reconstruction requires the full 2D table, not just the last row.