Stage 1 · Code
Dynamic Programming
Tabulation
Bottom-up DP with table filling.
Tabulation Concept
Tabulation = bottom-up DP. Build table iteratively from base cases up to target. Fill table in order where dependencies are already computed. No recursion, no call stack.
Fibonacci Tabulation
Space Optimization
If recurrence only depends on last k values, keep only those k variables. Fibonacci needs last 2 → O(1) space.
Determining Fill Order
Dependency graph determines fill order. For 1D: left→right (if dp[i] needs dp[i-1]) or right→left. For 2D: row by row, col by col, or diagonal. Key: when computing dp[i], all dependencies must be filled.
| Recurrence | Fill Order |
|---|---|
| dp[i] = dp[i-1] + dp[i-2] | i = 2..n (left to right) |
| dp[i] = dp[i+1] + dp[i+2] | i = n-2..0 (right to left) |
| dp[i][j] = dp[i-1][j] + dp[i][j-1] | Row 0..m, Col 0..n (top-left to bottom-right) |
| dp[i][j] = dp[i+1][j] + dp[i][j+1] | Row m-1..0, Col n-1..0 (bottom-right to top-left) |
| dp[i][j] = dp[i-1][j-1] + ... | Diagonals (i+j constant) |
Mark this lesson complete to store local progress and unlock a cleaner resume path the next time you visit.