Dynamic Programming
Dynamic Programming (DP) solves complex problems by breaking them into simpler overlapping subproblems. It’s essential for optimization problems where you need to find the best solution among many possibilities.Core Concepts
When to Use DP
- Optimal Substructure: Solution to problem contains solutions to subproblems
- Overlapping Subproblems: Same subproblems solved multiple times
- Decision at each step: Choose between multiple options
- Optimization: Minimize/maximize some value
DP Approaches
- Top-Down (Memoization): Recursive with caching
- Bottom-Up (Tabulation): Iterative, fill table
- Space Optimization: Reduce from O(n) to O(1)
Key Patterns & Techniques
1. Linear DP (1D)
- State depends on previous elements
- Examples: Fibonacci, house robber, stock prices
- Usually O(n) time, O(n) or O(1) space
2. Grid DP (2D)
- State depends on 2D position
- Examples: Unique paths, min path sum
- Usually O(m*n) time and space
3. Knapsack Pattern
- Include/exclude decision for each item
- 0/1 knapsack, subset sum
- O(n * capacity) time
4. String DP
- Subsequence, substring problems
- LCS, edit distance, word break
- Often O(n*m) for two strings
Common Approaches
Basic DP Template (Top-Down)
Basic DP Template (Bottom-Up)
Stock Buy/Sell (Linear DP)
Word Break (String DP)
Maximum Subarray (Kadane’s Algorithm)
Stock III (Multiple States)
Problems in This Category
018. Best Time to Buy and Sell Stock
Easy | Frequency: 76.4%Linear DP - track minimum price and maximum profit in single pass.
087. Maximum Subarray
Medium | Frequency: 32.0%Kadane’s algorithm - classic DP for maximum contiguous sum.
075. Word Break
Medium | Frequency: 40.7%String DP - check if string can be segmented into dictionary words.
099. Best Time to Buy and Sell Stock III
Hard | Frequency: 32.0%State machine DP - track multiple transaction states.
055. Subsets
Medium | Frequency: 47.0%Backtracking/DP - generate all possible subsets.
Complexity Characteristics
| Pattern | Time Complexity | Space Complexity | Optimization Possible |
|---|---|---|---|
| 1D Linear DP | O(n) | O(n) → O(1) | Yes, track last k states |
| 2D Grid DP | O(m*n) | O(m*n) → O(n) | Yes, only need prev row |
| Knapsack | O(n*W) | O(n*W) → O(W) | Yes, 1D array |
| String (LCS) | O(m*n) | O(m*n) → O(min(m,n)) | Yes, track prev row |
| Tree DP | O(n) | O(h) | No, need recursion stack |
Interview Tips
Pattern Recognition
- “Maximum/minimum profit” → DP with state tracking
- “Count ways to…” → DP counting paths/combinations
- “Can you reach/form…” → Boolean DP
- “Longest/shortest subsequence” → String DP
- “Contiguous subarray max/min” → Kadane’s algorithm
- “Include/exclude decision” → Knapsack pattern
Common Mistakes
- Not identifying overlapping subproblems
- Wrong base cases
- Off-by-one errors in indices
- Not considering all transitions
- Forgetting to optimize space
Key Insights
- Identify state: What information do you need to solve subproblem?
- Define recurrence: How does state i relate to previous states?
- Base cases: What are the simplest subproblems?
- Order of computation: Bottom-up must compute in right order
- Space optimization: Often only need last k states, not entire array
DP vs Greedy vs Divide & Conquer
| Approach | Overlapping Subproblems | Optimal Substructure | When to Use |
|---|---|---|---|
| DP | Yes | Yes | Multiple optimal choices, need all subproblems |
| Greedy | No | Yes | Local optimal → global optimal |
| D&C | No | Yes | Independent subproblems |
DP Patterns Summary
1. Fibonacci/Climbing Stairs
2. House Robber
3. Longest Increasing Subsequence
4. Coin Change
5. Edit Distance
Advanced Patterns
State Machine DP
Digit DP
Bitmask DP
Space Optimization Example
Pro Tip: Start with top-down memoization (easier to write), then optimize to bottom-up if needed. Always check if you can reduce space complexity by only keeping last k states instead of entire DP array.