DP for Beginners
Read a little, play a little. No scary maths, and no rush.
Stop recomputing what you already know
Compute fib(5) naively and you compute fib(3) twice, fib(2) three times — the work explodes exponentially because subproblems overlap. Dynamic programming is one idea: solve each distinct subproblem once, remember the answer, reuse it. That's it. That's the whole trick behind the scariest-sounding topic in interviews.
Climb stairs: DP wearing a disguise
You climb 1 or 2 steps at a time; how many ways to reach step n? To land on step i, you came from i−1 or i−2 — so ways[i] = ways[i−1] + ways[i−2]. Sound familiar? It's Fibonacci. And you only ever need the last two values:
def climb(n):
a, b = 1, 1 # ways to reach steps 0 and 1
for _ in range(n - 1):
a, b = b, a + b # slide the window forward
return b
O(n) time, O(1) space. The "table" collapsed into two variables — always look for that compression at the end.
House robber: adding a choice
Houses in a row hold cash; rob one and its neighbors lock down. At each house you choose: skip it (keep previous best) or rob it (its cash + best from two doors down):
# dp[i] = max(dp[i-1], dp[i-2] + nums[i])
Same skeleton as stairs, one max added. Most beginner DP is exactly this: yesterday's pattern plus one small twist.
The 5-step recipe
- State: what does
dp[i]mean, in words? - Recurrence: how does
dp[i]build from smaller answers? - Base: the trivial cases (
dp[0],dp[1]). - Order: fill small → large (bottom-up beats recursion + memo for interviews).
- Compress: keep only the rows you actually reread.
House robber, traced
Houses hold [2, 7, 9, 3, 1]. Fill the best total up to each house:
- House 0: 2.
- House 1: max(2, 7) = 7. Skip house 0 and take 7.
- House 2: max(7, 2 + 9) = 11. Rob houses 0 and 2.
- House 3: max(11, 7 + 3) = 11. Robbing house 3 does not beat what we have.
- House 4: max(11, 11 + 1) = 12.
The answer is 12, from houses 0, 2, and 4 (2 + 9 + 1).
Top-down or bottom-up?
There are two ways to write the same solution. Top-down keeps the natural recursive version and adds a cache, for example Python's functools.lru_cache, so each subproblem is computed once. Bottom-up fills a table from the smallest case upward, as in the examples above. Both are O(n) here. Top-down is often easier to derive from the recurrence; bottom-up avoids recursion limits and makes the space compression obvious.
Recognizing a DP problem
Signals: "how many ways," "minimum cost," "maximum value," or "is it possible," together with a choice at each step whose best outcome depends on smaller versions of the same problem.
A good test is to try the obvious greedy choice and look for a counterexample. With coins of 1, 3, and 4 and a target of 6, greedily taking the largest coin gives 4 + 1 + 1, three coins. The best answer is 3 + 3, two coins. When greedy fails like this, DP is usually the fix.
The next rungs
After stairs and house robber, the standard progression is coin change (fewest coins), longest increasing subsequence, unique paths in a grid (a two-dimensional table), and 0/1 knapsack. Each adds one new idea to the same five-step recipe.
Remember this
- Overlapping subproblems + optimal structure = reach for DP.
- Stairs → robber → knapsack is the standard difficulty ladder. Climb it in order.
Check your understanding
5 questions · first-try answers earn the most XP
My notes
Saved in this browser. Highlight a line above and save it, or write it in your own words.
Nothing saved yet. Your highlights will live here.
References
Finished reading?
Ticking it here also ticks the chapter in the sidebar, the section count and your streak — it is all one number.
Related chapters
Practice this pattern elsewhere
More problems to practise on external judges. Both links open the site home rather than a specific set, so you can pick problems for this pattern.
Spotted a mistake or want a topic covered? Report an issue