Dynamic Programming
Lesson 19 of 20Fill a 4×6 knapsack table row by row and tabulate Fibonacci — every subproblem solved once, then reused forever.
Recursion, with a memory
Dynamic programming is recursion that refuses to repeat itself: solve each subproblem once, write the answer down, and look it up forever after.
The bill naive recursion pays
Naive fib(6) recomputes fib(2) five times and fib(3) three times. A cache turns that exponential tree into a straight line.
Two ingredients
Overlapping subproblems + optimal substructure. When both hold — fib, knapsack, LCS — DP applies; when they do not, it misfires.
Define the state
dp[i][w] = the best value using the first i items within capacity w. Write that one sentence down before any code.
Write the recurrence
Skip item i → dp[i-1][w]; take it → value[i] + dp[i-1][w - weight[i]]. The cell keeps the max of the two.
Fill in dependency order
Row by row, left to right — every cell reads only cells that already exist. The bottom-right corner is the answer.
Knapsack fills the table
Items (w=2, v=3), (w=3, v=4), (w=4, v=5), capacity 5 — watch 24 cells fill in dependency order.
dp[i][w] = best value from the first i items within capacity w. Row 0 is the base case: no items → 0 everywhere.
Recurrence to result
Five moves turn any brute-force recursion into a table that never repeats work.
Walkthrough
The knapsack table builder and the Fibonacci tabulation — dependencies first, answers after.
1public class Knapsack {2public static void main(String[] args) {3int[] weight = {2, 3, 4};4int[] value = {3, 4, 5};5int cap = 5;6int[][] dp = new int[4][6];78for (int i = 1; i <= 3; i++) {9for (int w = 0; w <= cap; w++) {10dp[i][w] = dp[i - 1][w];11if (weight[i - 1] <= w) {12int take = value[i - 1] + dp[i - 1][w - weight[i - 1]];13if (take > dp[i][w]) dp[i][w] = take;14}15}16}17System.out.println("best value: " + dp[3][5]);18}19}
weight = [2, 3, 4] and value = [3, 4, 5] — three items; item i lives at index i-1.
Fibonacci tabulated
fib[0..6] filled left to right — each cell written exactly once, never recomputed.
fib[i] = fib[i-1] + fib[i-2]. Two base cases first, then fill left to right — each cell is read, never recomputed.
Predict the output
Coin combinations, tabulated bottom-up — trace the ways array by hand.
1public class Predict {2public static void main(String[] args) {3int[] coins = {1, 2, 5};4int[] ways = new int[6];5ways[0] = 1;6for (int c = 0; c < 3; c++) {7for (int a = coins[c]; a <= 5; a++) {8ways[a] += ways[a - coins[c]];9}10}11System.out.println(ways[5] + " combinations");12}13}
Hint: The coin loop sits OUTSIDE the amount loop, so order does not matter — these are combinations. ways[5] counts 1+1+1+1+1, 1+1+1+2, 1+2+2 and a single 5.
Concept check
Click a term, then click its meaning to lock the pair.
Key takeaways
DP is recursion with a memory: overlapping subproblems get solved exactly once.
Define the state in one sentence before writing any code — dp[i][w] means what you say it means.
The knapsack recurrence: dp[i][w] = max(dp[i-1][w], value + dp[i-1][w - weight]).
Fill order matters: row by row so every cell reads cells that already exist.
Fibonacci collapses to two variables — O(n) time, O(1) space.
The 1D knapsack loops capacity downward; looping upward would reuse an item twice.