The Disappearing Table

You've built DP tables. You know the formula, the base cases, the iteration order.

But have you ever looked at a finished table and wondered: “Did I really need ALL of those cells?”

That 2D table has rows and rows of numbers. Some of them matter. Most of them... well, you're about to find out.

Make It Vanish

1 / 4

0/1 Knapsack — Full Table

5 items, capacity 8. Fill cells in row 3 to see how the table works.

54 cells(100%)
w=0
w=1
w=2
w=3
w=4
w=5
w=6
w=7
w=8
row 0
no items
row 1
item 0 (w=2, v=3)
row 2
item 1 (w=3, v=4)
row 3
item 2 (w=4, v=5)
row 4
item 3 (w=5, v=7)
row 5
item 4 (w=1, v=1)
fill cells in row 3

The Pattern

Two ideas from this lesson show up everywhere in DP optimization:

Row dependency: If each row only reads the row directly above, you never need more than two rows. This applies to knapsack, edit distance, LCS, and most 2D DP problems.

Iteration direction: When you compress to a single row, direction determines correctness. In 0/1 knapsack, backward iteration prevents an item from being counted twice. In unbounded knapsack, you want forward iteration — same item, multiple uses.

Space optimization is not a trick. It's a consequence of understanding your recurrence's dependency pattern.