Pattern #23
Two-Dimensional DP
EssentialState depends on two changing indices (grid, two strings, two pointers in DP).
When to use
Paths in a grid, edit distance, LCS, or any recurrence with two parameters i and j.
Recognition cues
- Unique paths / min path sum
- Edit distance / LCS
- dp[i][j] from neighbors
Common pitfalls
- Wrong base row/column initialization
- Iterating in an order that reads unfilled cells
- Confusing inclusive lengths vs indices
90-second recognition drill
Which pattern fits best?
- Unique paths / min path sum
- Edit distance / LCS
- dp[i][j] from neighbors
Interactive
Mental model
A full worked walkthrough of the invariant. Pause, scrub the dots, or use ← →. Aim to narrate each step yourself.
Step 1 of 8
c0c1c2
r0
1
1
1
r1
1
?
?
r2
1
?
?
Statebase=row 0 & col 0 = 1
dp[r][c]=up+left
How to think about it
dp[i][j] = answer for a subproblem on prefix i and j (or cell (i,j)). Fill by increasing i+j or scanning rows. Often the answer sits at dp[m][n] or dp[m-1][n-1].
Template shapes
| Shape | Core move | Example |
|---|---|---|
| Unique paths | One rolling row. dp[c] holds the ways from above, and adding dp[c - 1] brings in the ways from the left |
LC 62 |
| Obstacles | The same rolling row. An obstacle writes 0 and contributes nothing to any path through it | LC 63 |
| Minimum path | In place. grid[i][j] adds min(up, left). The first row and the first column each have one predecessor |
LC 64 |
| Edit distance | Borders are dp[i][0] = i and dp[0][j] = j. Equal characters copy the diagonal. Otherwise the cell is 1 + min(insert, delete, replace). A rolling row keeps the previous row |
LC 72 |
| Maximal square | On a '1', the side is 1 + min(up, left, diagonal). A '0' stays 0. The answer is the largest side, squared |
LC 221 |
| LCS | A rolling row, and the diagonal is saved before it is overwritten. Equal characters take that diagonal plus one. Otherwise the cell is the max of skipping either character | LC 1143 |
Complexity baseline
O(m·n) time. A full table is O(m·n) space. The rolling row on LC 62, LC 63, LC 72, and LC 1143 is O(min(m, n)) extra space. LC 64 writes into the grid.
From template to problem
- A path count can live in one row. The cell already holds the ways from above, and adding the left neighbor brings the other direction (LC 62). An obstacle writes 0 (LC 63).
- Minimum path writes into the grid. The first row and the first column have a single predecessor (LC 64).
- Edit-distance borders are the pure inserts and deletes. Save the diagonal before a rolling row overwrites it (LC 72). LCS saves that same diagonal (LC 1143).
- A square’s side is 1 plus the minimum of the three neighbors, and the answer is that side squared (LC 221).
Template
Same skeleton in TypeScript, Python, and C#. Adapt the invariant; keep the structure.
Two-Dimensional DP · Template
/** 2D DP template: unique paths (right/down only). dp[c] rolled per row. */
export function uniquePaths(m: number, n: number): number {
const dp = new Array<number>(n).fill(1);
for (let r = 1; r < m; r++) {
for (let c = 1; c < n; c++) dp[c] = dp[c]! + dp[c - 1]!;
}
return dp[n - 1]!;
}
/** 2D DP template: unique paths (right/down only). dp[c] rolled per row. */
export function uniquePaths(m: number, n: number): number {
const dp = new Array<number>(n).fill(1);
for (let r = 1; r < m; r++) {
for (let c = 1; c < n; c++) dp[c] = dp[c]! + dp[c - 1]!;
}
return dp[n - 1]!;
}
#StatusProblemTypeDifficultyDone
- 1#62 Unique PathsGuidemedium
- 2#63 Unique Paths IIGuidemedium
- 3#64 Minimum Path SumGuidemedium
- 4#72 Edit DistanceGuidemedium
- 5#221 Maximal SquareGuidemedium
- 6#1143 Longest Common SubsequenceGuidemedium