DP - 2D: Two-dimensional dynamic programming
Goal: Learn the pattern first, then apply it to the 11 roadmap problems in this chapter.
When this pattern appears
The state needs two indices such as prefixes, two sequences, or intervals.
The reusable idea
Let dp[i][j] have a precise prefix/range meaning. Draw a tiny table and derive neighbors.
A deliberate problem-solving routine
- Write the input and output in plain language. Identify whether order, duplicates, or mutation matter.
- Work a tiny example by hand; label the state you wish you had at each step.
- Choose the structure or invariant that stores that state.
- Establish a brute-force baseline, then show exactly which repeated work the pattern removes.
- State the invariant before the loop, after each iteration, and at termination.
- Test smallest, boundary, duplicate, and adversarial cases.
Watch the animated pattern
The diagram above this lesson animates the core state changes for this chapter. Use pause and step to predict what moves next. The same invariant should explain each application, even when the problem story changes.
Questions to ask yourself
What do the two coordinates represent, and which smaller states lead here?
Common failure modes
Empty-prefix rows/columns, iteration direction, interval boundaries, and count overflow.
Chapter problems from the workbook
- #113 · Unique Paths — Day 67, Medium. Open problem ↗
- #114 · Longest Common Subsequence — Day 67, Medium. Open problem ↗
- #115 · Coin Change II — Day 68, Medium. Open problem ↗
- #116 · Target Sum — Day 68, Medium. Open problem ↗
- #117 · Best Time to Buy and Sell Stock with Cooldown — Day 69, Medium. Open problem ↗
- #118 · Edit Distance — Day 69, Medium. Open problem ↗
- #119 · Interleaving String — Day 71, Medium. Open problem ↗
- #120 · Longest Increasing Path in a Matrix — Day 71, Hard. Open problem ↗
- #121 · Distinct Subsequences — Day 72, Hard. Open problem ↗
- #122 · Burst Balloons — Day 72, Hard. Open problem ↗
- #123 · Regular Expression Matching — Day 73, Hard. Open problem ↗
Interview explanation
A concise explanation has four parts: the key observation, the maintained invariant, why each step preserves it, and the time/space complexity. Avoid naming a pattern as a substitute for proving the step is safe.