Dynamic programming never solves the same subproblem twice — it fills a table once and reads the answer off the corner.
1 The core trick
Solve each subproblem once and store it — overlapping subproblems are the tell that DP applies.
2 Two styles
Memoization caches recursive calls top-down; tabulation fills a table bottom-up from base cases.
3 The knapsack table
dp[i][c] = best value from the first i items within capacity c — 24 cells for 3 items, cap 5.
4 Climbing stairs
ways[i] = ways[i-1] + ways[i-2] — Fibonacci in hiking boots: 1, 1, 2, 3, 5.
5 The win
Naive knapsack branches exponentially; the table flattens it to O(n × cap) — and beats greedy on coin {1,3,4}.
Why DP wins
The core trick1Solve each subproblem once and store it — overlapping subproblems are the tell that DP applies. Two styles2Memoization caches recursive calls top-down; tabulation fills a table bottom-up from base cases. The knapsack table3dp[i][c] = best value from the first i items within capacity c — 24 cells for 3 items, cap 5. Climbing stairs4ways[i] = ways[i-1] + ways[i-2] — Fibonacci in hiking boots: 1, 1, 2, 3, 5. The win5Naive knapsack branches exponentially; the table flattens it to O(n × cap) — and beats greedy on coin {1,3,4}. 5 building blocks make up Why DP wins
Capacity 5, items A(w2 v3), B(w3 v4), C(w4 v5). Row by row, cell by cell — each computed once.
cap 0cap 1cap 2cap 3cap 4cap 5no items······
+A w2 v3······
+B w3 v4······
+C w4 v5······
1 / 120/1 knapsack, capacity 5. dp[i][c] = best value using the first i items within capacity c — the table starts empty.
Five moves take a DP problem from a vague wish to a filled table and a number.
The tabulation loop in real C++ — a smaller knapsack (cap 4, two items) traced cell by cell.
maincap = 4, two items: A (w2 v3) and B (w3 v4).
Memory
cap= 4n= 2w= {2, 3}v= {3, 4}
Console
output appears as the program runs…
One stair at a time or two — the ways table sums the two cells behind it.
1 / 10dp[i] = number of ways to reach stair i using 1- or 2-steps. Five cells, one per stair 0–4.
Six stairs, steps of 1 or 2 — tabulate the ways and read the last cell.
Your predicted output — one line per console line
Hint: Every cell sums the two before it: 1, 1, 2, 3, 5, 8. ways[5] = ways[4] + ways[3] = 5 + 3.
Click a term, then click its meaning to lock the pair.
8 conceptsTap a card to expand ?overlapping subproblemsdrops into place
?tabulationdrops into place
?memoizationdrops into place
?dp[i][c]drops into place
?max(skip, take)drops into place
?base casedrops into place
?ways[i] = w[i-1] + w[i-2]drops into place
?O(n × cap)drops into place
01DP solves each subproblem once and stores it — overlapping subproblems are the signal.
02Tabulation fills bottom-up; memoization caches top-down recursion.
03Every table starts at base cases: row 0 of knapsack is all zeros.
04Knapsack recurrence: dp[i][c] = max(dp[i-1][c], v[i] + dp[i-1][c-w[i]]).
05Fill order matters — a cell may only read cells that are already final.
06dp[C][5] = 7 here: items A + B, weight 5, value 7 — in O(n × cap).