Breaking Down Dynamic Programming
DP is often considered the hardest topic in interviews, but it boils down to two concepts: Overlapping Subproblems and Optimal Substructure.
Approaches
- Top-Down (Memoization): Write a recursive function and cache the results of subproblems in a hash map or array. Easier to conceptualize.
- Bottom-Up (Tabulation): Build a table iteratively from the base cases up to the target. Avoids recursion stack overhead.
Classic DP Problems
- 0/1 Knapsack Problem
- Longest Common Subsequence
- Matrix Chain Multiplication