Dynamic programming is recursion with a memory. When a problem has (1) overlapping subproblems, the same smaller cases get solved repeatedly, and (2) optimal substructure, the best answer is built from best answers to smaller cases, you can compute each subproblem ONCE, store it, and reuse it. Naive recursive Fibonacci is O(2ⁿ) because fib(5) computes fib(3) twice, fib(2) three times... memoization makes it O(n) with three added lines.
Two styles, same substance. Top-down (memoization): keep the natural recursive structure, add a cache checked on entry. Bottom-up (tabulation): start from the base cases, fill a table in dependency order until you reach the target, no recursion, no stack limits, and usually obvious space savings (climbing stairs only ever needs the last two entries). interviewers often want bottom-up; learners usually find top-down easier to derive first. Learn both; derive top-down, then optimize.
One problem, three ways
// Problem: climb n stairs, 1 or 2 steps at a time. How many distinct ways?
// Recurrence: ways(n) = ways(n-1) + ways(n-2) (last step was 1 or 2)
// 1) Naive recursion, O(2ⁿ). Same subproblems recomputed exponentially.
function waysNaive(n) {
if (n <= 1) return 1;
return waysNaive(n - 1) + waysNaive(n - 2);
}
// 2) Top-down (memoization), O(n) time, O(n) space. Recursion + cache.
function waysMemo(n, memo = new Map()) {
if (n <= 1) return 1;
if (memo.has(n)) return memo.get(n);
const result = waysMemo(n - 1, memo) + waysMemo(n - 2, memo);
memo.set(n, result);
return result;
}
// 3) Bottom-up (tabulation), O(n) time, O(1) space. Fill a table (or two vars).
function waysTab(n) {
if (n <= 1) return 1;
let prev2 = 1, prev1 = 1; // ways(0), ways(1)
for (let i = 2; i <= n; i++) {
const cur = prev1 + prev2; // the recurrence, now a loop
prev2 = prev1;
prev1 = cur;
}
return prev1;
}The DP derivation recipe
- 1
Define the subproblem precisely
"ways(i) = number of ways to climb i stairs", "minCoins(k) = fewest coins summing to k". A precise definition is 60% of the work.
- 2
Find the recurrence
How does answer(i) combine smaller answers? For coin change: minCoins(k) = 1 + min over coins c of minCoins(k − c). Write it down before coding.
- 3
Identify base cases
ways(0) = 1 (one way: do nothing), minCoins(0) = 0. Wrong base cases poison everything above them.
- 4
Choose an order
Top-down with memo = recursion order (automatic). Bottom-up: fill so every dependency is ready, usually smallest to largest.
- 5
Optimize space
If the recurrence only looks back a fixed window, keep just that window (two variables beat an array here).
Bottom-up coin change (minimum coins)
function minCoins(coins, amount) {
const dp = new Array(amount + 1).fill(Infinity);
dp[0] = 0; // base: 0 coins make 0
for (let k = 1; k <= amount; k++) {
for (const c of coins) {
if (c <= k && dp[k - c] + 1 < dp[k]) {
dp[k] = dp[k - c] + 1; // best last-coin choice for k
}
}
}
return dp[amount] === Infinity ? -1 : dp[amount];
}
minCoins([1, 3, 4], 6); // 2 (3 + 3), beats greedy's 3