Demystifying Dynamic Programming
Imagine you’re counting the number of stairs in a building. You count to 10. If someone asks, "How many stairs would there be if we added one more?", you don't go back to the bottom and start over. You just say "11."
That is DP: Remembering the past to solve the future. Here's a break down of the "DP Mental Model" that helped me to stop memorizing solutions and start building them.
Thinking Tree Model
Here's my 5-step mental model to map any problem to a known DP pattern.
1. The Decision Tree
Before you write a single line of code, follow this flow:
Is there a "Choice"? Am I picking an item, taking a step, or choosing a path? If yes, it’s likely an optimization problem.
Does the future depend on the past? If I solve a small version of this (e.g., a shorter string or a smaller sum), does that help me solve the big one? (Optimal Substructure).
Am I repeating work? If you draw a recursion tree and see the same calculation twice, you’ve found the "Overlapping Subproblems" that DP is built to fix.
2. The Pattern Matching Guide
Most DP problems fall into a few "families." Once you recognize the family, the solution follows a template.
The Fibonacci Style (Linear DP)
The Vibe: You need the previous one or two results to get the current one.
Examples: Climbing Stairs, House Robber.
Thinking: dp[i] = dp[i-1] + dp[i-2]The Knapsack (0/1 or Unbounded)
The Vibe: You have a "bag" with a limit and items with "weights" and "values." Do you take the item or leave it?
Examples: Partition Equal Subset Sum, Coin Change.
Thinking: max(include_item, exclude_item)Longest Common Subsequence (Two Strings)
The Vibe: Comparing two sequences or strings.
Examples: Edit Distance, Longest Palindromic Subsequence.
Thinking: Use a 2D grid where dp[i][j] represents the relationship between the first i characters of String A and first j of String B.Longest Increasing Subsequence
The Vibe: Finding a trend within a single array.
Thinking: Every element looks back at all previous elements to see which one it can "attach" itself to.Grid/Matrix Problems
The Vibe: You are moving through a 2D world (usually Top-Left to Bottom-Right).
Examples: Unique Paths, Minimum Path Sum.
Thinking: dp[i][j] = current_val + min(top_cell, left_cell)
3. The "State" Checklist
Think of the State as a Snapshot. Ask yourself: What changes? (Is it the index of the array? The remaining capacity of the bag? The current sum?) These "changing things" are your variables. If one thing changes, you need a 1D array (dp[i]). If two things change, you need a 2D matrix (dp[i][j]).
4. The "Reverse-Engineered" Identification Guide
Sometimes, the easiest way to find the right pattern is to look at the Goal and the Rules of the problem. If you're stuck, ask yourself these two questions:
- What is the Desired Output? Match your goal to these standard DP objectives:
"Find the Longest..."
...shared between two things? → String/LCS
...increasing order? → LIS
...that reads the same forward and backward? → LPS"Find the Maximum/Minimum Path...
" On a grid? → Grid DP .
In a linear sequence? → Linear DP ."Can we achieve sum X?" or "Max value within capacity?" → Knapsack DP
2. What are the "Movement" Rules?How the problem allows you to move defines your Recurrence Relation:
Sequential: If you only care about the element right before the current one (e.g., i−1, i−2), it's Linear .
Directional: If you move Right and Down, it's Grid .
Pick or Skip: If you must decide whether to include an item or leave it behind, it's Knapsack .
Window/Sub-segment: If you are shrinking or expanding a range (e.g., from i to j), it's Partition/LPS
5. The "FAST" Strategy for Coding
F - Find the recursive relation (The "brute force").
A - Add Memoization (The "top-down" fix).
S - Switch to Tabulation (The "bottom-up" table).
T - Tweak for Space Complexity (Can we use just two variables instead of a whole array?).
Check out my GitHub - https://github.com/saipriya-m-ravi/DS_ALGO/tree/main/DP for the full pattern breakdown.
Warning: the organization is a bit chaotic because I prioritized coding over cleaning. Have a look—if you can navigate the chaos, there's some real gold in there!
