Dynamic Programming Basics
Part of the Python Roadmap
In short
Dynamic programming sounds scary, but the core idea is simple: if you've already solved a smaller version of the problem, write the answer down and reuse it. That turns impossibly slow recursion into fast, clean code.
Dynamic programming solves problems with overlapping subproblems by storing answers you have already computed, either top-down with memoisation (lru_cache) or bottom-up with a table.

Think of it like this
The maths teacher's rough notebook
When Mrs Iyer works out a long problem, she writes each small result in the margin. When the same sub-sum turns up again three steps later, she just reads it off instead of recalculating. DP is that margin: store each subproblem's answer once, reuse it every time.
Watch it happen
Ways to climb 5 stairs, 1 or 2 steps at a time
1/5ways[n] = ways[n-1] + ways[n-2]: your last move was either 1 step or 2 steps. Base cases: ways[0] = ways[1] = 1.
The big ideas
What you need to know
Overlapping subproblems
Plain recursion for ways(30) recomputes ways(10) thousands of times. DP computes it once.
Top-down: memoise
Write the natural recursion and add @lru_cache. Python stores each result automatically.
Bottom-up: tabulate
Fill a list from the smallest case upwards. No recursion depth limits to worry about.
Define the state
The hard part is saying what best[i] means in one sentence. Once that's clear, the formula usually follows.
How it works
Solving any DP problem
- 1
Define the state
What does dp[i] mean? For example, the fewest coins needed to make amount i.
- 2
Find the recurrence
How does dp[i] depend on smaller states? Try each coin: dp[i - coin] + 1.
- 3
Set base cases
dp[0] = 0: zero coins make zero rupees.
- 4
Choose an order
Fill from small to large so every value you need is already computed.
See it in code
Walk through the code
1from functools import lru_cache2 3 4@lru_cache(maxsize=None)5def ways_to_climb(n: int) -> int:6 if n <= 1:7 return 18 return ways_to_climb(n - 1) + ways_to_climb(n - 2)9 10 11def min_coins(coins: list[int], amount: int) -> int:12 impossible = amount + 113 best = [0] + [impossible] * amount14 for value in range(1, amount + 1):15 for coin in coins:16 if coin <= value:17 best[value] = min(best[value], best[value - coin] + 1)18 return best[amount] if best[amount] != impossible else -119 20 21print(ways_to_climb(5)) # 822print(ways_to_climb(80)) # instant, thanks to the cache23print(min_coins([1, 7, 10], 14)) # 2, using 7 + 71/4Natural recursion plus @lru_cache. Without the cache, ways_to_climb(80) would effectively never finish.
Top-down with a cache, and bottom-up with a table. Both store each subproblem's answer once.
Save yourself the pain
Common mistakes
Oops
Assuming taking the biggest coin first always gives the fewest coins.
Do this instead
Greedy works for ₹1, 2, 5, 10 but fails for coins like 1, 7, 10. DP checks every option.
Oops
Using @lru_cache on a function that takes a list argument.
Do this instead
Cached arguments must be hashable. Pass a tuple, or an index into data stored outside the function.
Oops
Jumping straight to code without defining the state.
Do this instead
Write one sentence for what dp[i] means and the recurrence first. The code then takes a few lines.
Your mission
Three starter DP problems
Quick check
Why is plain recursive ways_to_climb(40) so slow without a cache?
Go deeper
- 1Solve climbing stairs both with @lru_cache and with a bottom-up list.
- 2Try the coin change problem using rupee coin values.
- 3Write the recurrence in words before you write any code.
Also part of these roadmaps
More topics at this stop
Stuck on this topic? Ask an Insider
Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.