Dynamic Programming
Dynamic programming solves a problem by combining answers to overlapping subproblems, storing each answer so you never compute it twice.
Detailed Explanation
Divide-and-conquer splits work into independent pieces (merge sort). Dynamic programming splits work into pieces that share smaller pieces. Fibonacci is the toy: F(n) needs F(n-1) and F(n-2), and those two share almost the whole tree. Naive recursion is exponential. Store F(k) the first time you compute it and the tree collapses to O(n).
Two styles.
Memoization is top-down. You write the recursive function and cache results in a Map or an array keyed by the subproblem. The first call fills the cache as it returns. This is natural when the state is sparse (you will not visit every possible key).
Tabulation is bottom-up. You decide an order that only uses already-filled cells, then loop. dp[i] = dp[i - 1] + dp[i - 2] is Fibonacci. This is natural when the state is dense and you want O(1) extra if you can roll the array.
A DP solution has a state (what you must remember to finish the problem), a recurrence (how states combine), and base cases. If you cannot name those three, you are not ready to code. Classic states: index in an array, remaining capacity, a bitmask of used items, a pair of indexes in two strings (edit distance, LCS).
JavaScript specifics: object keys stringify, so a state of [i, cap] as map.set([i, cap], v) will not hit. Use map.set(${i},${cap}, v) or a nested array dp[i][cap]. Recursion plus memo still consumes call-stack depth; a 10k index walk should be a loop. Array(n).fill([]) shares one inner array; fill with a loop or Array.from.
What it costs you: memory for the table, and over-DP. If subproblems do not overlap, you just wasted space. If you only need the last row, keep two rows or two variables. Interview DP is often O(n) or O(n * cap); if you invent an exponential state, look for a greedy proof or a different state.
Reach for DP when you see "number of ways", "minimum cost", "can you reach", and overlapping prefixes or subsets. Reach for greedy when a local choice is provably safe. Reach for backtracking when you must list every solution, not count or optimise.
Code Examples
function fib(n, memo = new Map()) {
if (n <= 1) return n;
if (memo.has(n)) return memo.get(n);
const value = fib(n - 1, memo) + fib(n - 2, memo);
memo.set(n, value);
return value;
}
console.log(fib(10)); // => 55function fibTable(n) {
if (n <= 1) return n;
let a = 0;
let b = 1;
for (let i = 2; i <= n; i += 1) {
const next = a + b;
a = b;
b = next;
}
return b;
}
console.log(fibTable(10)); // => 55function coinWays(amount, coins) {
const dp = new Array(amount + 1).fill(0);
dp[0] = 1;
for (const coin of coins) {
for (let x = coin; x <= amount; x += 1) {
dp[x] += dp[x - coin];
}
}
return dp[amount];
}
console.log(coinWays(5, [1, 2, 5])); // => 4Common Gotchas
Array or object keys as `Map` keys use reference identity
stringify the state or use a nested table.
`Array(n).fill([])` reuses one array
every row aliases the others. Use `Array.from({ length: n }, () => [])`.
Memoized recursion still needs stack
tabulate when the longest chain of subproblems is huge.
Rolling arrays need the inner loop direction to match the recurrence
coin-change unbounded vs 0/1 knapsack iterate capacity in opposite directions.