1. Repeated subproblems
Dynamic programming applies when a problem has two properties: overlapping subproblems and optimal substructure. Overlapping means the recursive solution recomputes the same state; optimal substructure means the best answer to the whole problem is built from best answers to smaller ones.
Naive recursive Fibonacci is the smallest example. It computes F(n−1) and F(n−2), but F(n−2) is also inside F(n−1), and the same values are recomputed exponentially many times. The recursion tree is O(2ⁿ), even though there are only n distinct states.
2. Memoization
Memoization keeps the top-down recursion and adds a cache. Before computing a state, look it up; after computing it, store it. Each state is solved once, so the running time drops from O(2ⁿ) to O(n).
memo = {0: 0, 1: 1}
def fib(n):
if n in memo: return memo[n]
memo[n] = fib(n-1) + fib(n-2)
return memo[n]3. Tabulation
Tabulation fills a table from the base cases upward. There is no recursion and no cache lookup; the table itself is the memory. For Fibonacci you only need the last two values, so the table can shrink to O(1) space.
a, b = 0, 1
for i in range(2, n + 1):
a, b = b, a + b
return bThe two styles are equivalent in asymptotic time. Memoization is often easier to write from a recurrence; tabulation is often easier to optimize for space and iteration order.
4. Grid paths
Count the paths from the top-left to the bottom-right of a grid, moving only right and down. Let dp[r][c] be the number of paths to cell (r, c). Every path into a cell comes either from above or from the left, so the recurrence adds those two values. A blocked cell contributes zero.
The table has rows × cols states, and each state takes O(1) work. The total time is O(rows × cols). The answer is the bottom-right cell.
Example: 3×3 grid, no obstacles
First row and first column are all 1: there is only one straight-line route.
Interior cells are sums: 2, 3, 3, and the bottom-right is 6.
Block the centre cell and the table becomes 1,1,1 / 1,0,1 / 1,1,2: only two paths remain.
5. Reconstruction
Counting is only half the problem. To recover one optimal path, walk backward from the answer cell. At each step, compare the cell above and the cell to the left; move to the one that could have produced the current value. If the problem stores choices explicitly, the reconstruction is even simpler.
- Counting — store the number of paths.
- Optimization — store the best value (minimum cost, maximum reward).
- Reconstruction — store the choice, or backtrack using the table.
- Space — if you only need the final value, keep the current and previous row instead of the whole table.
When DP does not apply
If the subproblems are independent, divide and conquer is enough. If the state needs the entire history, the state space explodes and DP is the wrong tool. The art is choosing a state that captures exactly what the future needs.
6. Practice
Why is naive recursive Fibonacci O(2ⁿ) rather than O(n)?
How many paths are there through a 2×2 grid with right/down moves?
What does a blocked cell contribute to the grid-path recurrence?
When can the grid-path table be reduced to one row?
7. Resources
The DP playground lets you draw obstacles and watch the table fill. Change one cell and see how every later value depends on it; that dependency is the recurrence.
Continue the track
Open the playground for this topic
DP Playground →Draw obstacles on a grid and watch the path-count table fill with the number of routes to each cell.