Dynamic Programming
๐ Quick Reference
| Property | Value |
|---|---|
| Type | Optimization technique |
| Key Idea | Break into subproblems, store results, avoid recomputation |
| Requirements | Optimal substructure + overlapping subproblems |
| Approaches | Top-down (memoization) or Bottom-up (tabulation) |
| Best For | Optimization problems, counting problems |
๐ฎ Interactive Visualizer
Watch how DP builds solutions from smaller subproblems:
Loading visualizer...
- See Fibonacci with memoization - avoid redundant calls
- Watch the DP table fill for knapsack
- Observe how subproblems build on each other
- Compare top-down vs bottom-up approaches
๐ง Classic Problems
Fibonacci (Top-Down with Memoization)
JAVA(12 lines)CodeLoading syntax highlighter...
Fibonacci (Bottom-Up with Tabulation)
JAVA(27 lines)CodeLoading syntax highlighter...
0/1 Knapsack
JAVA(19 lines)CodeLoading syntax highlighter...
Longest Common Subsequence (LCS)
JAVA(16 lines)CodeLoading syntax highlighter...
Coin Change (Minimum Coins)
JAVA(15 lines)CodeLoading syntax highlighter...
๐ DP Patterns
Pattern 1: Linear DP
JAVA(13 lines)CodeLoading syntax highlighter...
Pattern 2: 2D Grid DP
JAVA(18 lines)CodeLoading syntax highlighter...
Pattern 3: String DP
JAVA(24 lines)CodeLoading syntax highlighter...
Pattern 4: Interval DP
JAVA(23 lines)CodeLoading syntax highlighter...
โ When to Use DP
Good Indicators
- "Minimum/maximum" - optimization problem
- "Count the ways" - counting problem
- "Can it be done?" - feasibility problem
- Overlapping subproblems - same states computed repeatedly
- Optimal substructure - optimal solution uses optimal sub-solutions
Common DP Problems
| Category | Examples |
|---|---|
| Sequence | Fibonacci, Climbing Stairs, LIS |
| Grid | Unique Paths, Minimum Path Sum |
| String | LCS, Edit Distance, Palindrome |
| Knapsack | 0/1 Knapsack, Coin Change |
| Interval | Matrix Chain, Burst Balloons |
| Tree | Tree DP, Binary Tree Maximum Path |
๐ Top-Down vs Bottom-Up
| Aspect | Top-Down (Memoization) | Bottom-Up (Tabulation) |
|---|---|---|
| Direction | Start from problem, recurse | Start from base cases |
| Computation | Only needed subproblems | All subproblems |
| Stack | Recursion overhead | Iterative |
| Debugging | Easier to understand | Easier to trace |
| Space Optimization | Harder | Often possible |
JAVA(16 lines)CodeLoading syntax highlighter...
โ ๏ธ Common Pitfalls
1. Wrong Base Case
JAVA(10 lines)CodeLoading syntax highlighter...
2. Wrong Recurrence Relation
JAVA(5 lines)CodeLoading syntax highlighter...
3. Index Out of Bounds
JAVA(5 lines)CodeLoading syntax highlighter...
4. Not Recognizing DP Problem
JAVA(6 lines)CodeLoading syntax highlighter...
๐ค Interview Tips
"Look for optimization/counting problems with overlapping subproblems. If you see the same recursive calls being made, it's likely DP. Common patterns: "minimum/maximum", "count ways", "is it possible".
"Top-down is often easier to derive (just add memoization to recursion). Bottom-up can be more efficient (no recursion overhead) and easier to optimize space. Start with top-down, convert to bottom-up if needed.
"Think about the last decision/step. What choices lead to the current state? Express the answer in terms of smaller subproblems. Draw decision trees if stuck.
"If dp[i] only depends on dp[i-1] and dp[i-2], you only need two variables, not an array. For 2D DP, if you only look at the previous row, use a 1D array.
๐ Series Navigation
DP from @tomaszjarosz/react-visualizers