What is Dynamic Programming?
Dynamic Programming solves complex problems by breaking them into overlapping subproblems and storing results to avoid redundant computation. It transforms exponential solutions into polynomial ones.Quick Recognition: If you see “optimal” (min/max), “number of ways”, or “can we achieve” combined with choices at each step, think DP!
Pattern Recognition Checklist
Use DP When
- Problem has optimal substructure
- Same subproblems solved repeatedly
- Need to find min/max/count
- Choices at each step affect result
- Can break into smaller similar problems
Don't Use When
- Greedy choice works (no need to try all)
- No overlapping subproblems
- Need all solutions (use Backtracking)
- Simple iteration is sufficient
When to Use
Optimal Substructure
Optimal solution contains optimal solutions to subproblems
Overlapping Subproblems
Same subproblems are solved multiple times
Counting Problems
Number of ways to achieve something
Optimization
Minimize/maximize under constraints
The IDEAL Framework
1
Identify
Recognize it’s a DP problem (optimal/count + choices + constraints)
2
Define
Define what dp[i] or dp[i][j] represents
3
Express
Write the recurrence relation (transition equation)
4
Analyze
Determine base cases and computation order
5
Look back
Optimize space if possible
Pattern Variations
1. 1D DP (Linear)
2. 2D DP (Grid/Sequence)
3. Knapsack Pattern
4. LCS Pattern (Two Strings)
5. House Robber (Cannot Take Adjacent)
6. Coin Change
Classic Problems
1. Climbing Stairs
1. Climbing Stairs
Pattern: 1D DP (Fibonacci-like)State: dp[i] = ways to reach step i
2. Longest Increasing Subsequence
2. Longest Increasing Subsequence
Pattern: 1D DP with O(n^2) or binary search O(n log n)State: dp[i] = LIS ending at index i
3. Edit Distance
3. Edit Distance
Pattern: 2D DP on two stringsState: dp[i][j] = min operations to convert s1[:i] to s2[:j]
4. Partition Equal Subset Sum
4. Partition Equal Subset Sum
Pattern: 0/1 Knapsack variantState: dp[i][sum] = can we make sum using first i elements?
Common Mistakes
Debugging Checklist
When your DP solution fails:1
Check State Definition
Is dp[i] clearly defined? Can you explain what it represents?
2
Check Base Cases
Have you initialized all necessary base cases correctly?
3
Check Recurrence
Does your transition cover all cases? Any edge cases missed?
4
Check Iteration Order
Are you computing dp[i] only after dp[i-1], dp[i-2], etc.?
5
Check Return Value
Are you returning the right cell? dp[n] vs dp[n-1]?
DP Pattern Categories
| Category | Examples | State Definition |
|---|---|---|
| Linear DP | Climbing Stairs, House Robber | dp[i] = answer for first i elements |
| Grid DP | Unique Paths, Min Path Sum | dp[i][j] = answer reaching (i,j) |
| String DP | LCS, Edit Distance | dp[i][j] = answer for s1[:i], s2[:j] |
| Knapsack | 0/1 Knapsack, Coin Change | dp[i][w] = answer using i items, capacity w |
| Interval DP | Matrix Chain, Burst Balloons | dp[i][j] = answer for interval [i,j] |
| Tree DP | House Robber III | dp[node] = answer for subtree at node |
Complexity Quick Reference
| Problem Type | Time | Space | Optimization |
|---|---|---|---|
| 1D DP | O(n) | O(n) → O(1) | Keep only last 2 values |
| 2D Grid DP | O(mn) | O(mn) → O(n) | Keep only previous row |
| String DP (LCS) | O(mn) | O(mn) → O(n) | Keep only previous row |
| Knapsack | O(nW) | O(nW) → O(W) | Single row iteration |
| Interval DP | O(n³) | O(n²) | Usually not optimizable |
Interview Problems by Company
- Easy
- Medium
- Hard
| Problem | Company | DP Type |
|---|---|---|
| Climbing Stairs | All | 1D Linear |
| Min Cost Climbing Stairs | Amazon | 1D Linear |
| Maximum Subarray | All | 1D (Kadane’s) |
| Best Time to Buy Stock | Amazon | 1D State Machine |
| House Robber | Google, Meta | 1D Linear |
Interview Tips
How to Explain Your Approach
How to Explain Your Approach
Script for interviews:
- “I notice this is asking for [optimal/count], which suggests DP.”
- “Let me define the state: dp[i] represents…”
- “The recurrence is: dp[i] = … because…”
- “Base case is dp[0] = … because…”
- “Time is O(…) and space is O(…), which I can optimize to…”
When Interviewer Says...
When Interviewer Says...
| Interviewer Says | You Should Think |
|---|---|
| ”Find minimum/maximum” | Optimization DP |
| ”Count the number of ways” | Counting DP |
| ”Can you achieve X?” | Boolean DP (true/false) |
| “Optimize your recursion” | Add memoization |
| ”Reduce space complexity” | Rolling array technique |
Top-Down vs Bottom-Up
Top-Down vs Bottom-Up
Top-Down (Memoization):
- Easier to write (natural recursion)
- Only computes needed subproblems
- Can have stack overflow for large inputs
- Usually faster (no recursion overhead)
- Easier to optimize space
- Must compute all subproblems
Practice Problems
| Problem | Difficulty | Link |
|---|---|---|
| Climbing Stairs | Easy | LeetCode 70 |
| House Robber | Medium | LeetCode 198 |
| Coin Change | Medium | LeetCode 322 |
| Longest Common Subsequence | Medium | LeetCode 1143 |
| Edit Distance | Medium | LeetCode 72 |
Practice Roadmap
1
Week 1: 1D DP
- Solve: Climbing Stairs, House Robber, Max Subarray
- Focus: State definition and transitions
2
Week 2: 2D Grid DP
- Solve: Unique Paths, Min Path Sum, Triangle
- Focus: Grid traversal patterns
3
Week 3: String DP
- Solve: LCS, Edit Distance, Palindrome Substring
- Focus: Two-string state definition
4
Week 4: Advanced
- Solve: Coin Change, Knapsack, Word Break
- Focus: Recognizing DP variants