2-D dynamic programming is the same idea as 1-D DP (solve each smaller version of the problem once and save the answer), but each answer is described by two numbers instead of one. So the saved answers form a grid (a table with rows and columns).
If you haven't read the 1-D DP primer yet, start there: everything in it still applies.
When do you need two numbers?
The state needs two numbers when the problem has two things that change independently:
| Problem | The two numbers | One saved answer means |
|---|---|---|
| Unique Paths | a row r and a column c | the number of paths into cell (r, c) |
| Longest Common Subsequence | a position i in text1, j in text2 | the answer for text1 from i and text2 from j |
Typical signs: a grid, or two strings / two arrays compared against each other.
The recipe is the same
- Define the state in one sentence: "
table[i][j]= ...". - Write the recurrence: ask "what was the last (or first) choice?" Each cell usually depends on a few neighbors: above, left, or diagonal.
- Base cases: usually the first row and column (or an extra "empty" row and column).
- Fill order: choose an order where every neighbor you need is already filled.
Which neighbors, and which order?
Draw the cell and arrows to the cells it reads from. The fill order must visit those cells first.
- Unique Paths reads from above and left: fill top to bottom, left to right.
- Longest Common Subsequence (as written here) reads from below, right, and the diagonal down-right: fill bottom to top, right to left.
The two-string pattern
For two strings, a very common recurrence compares the current characters:
if a[i] === b[j]: use the match, move both forward → table[i + 1][j + 1]
else: try skipping one from either string → table[i + 1][j], table[i][j + 1]Longest Common Subsequence, Edit Distance, and many other "compare two sequences" problems are variations of this.
Saving memory: keep one or two rows
A cell usually only reads from the current row and the row next to it. So instead of the whole m × n table, keep one or two rows: O(n) memory instead of O(m · n).
- Unique Paths uses a single row:
row[c] += row[c - 1]. Before the update,row[c]is still the value from above;row[c - 1]was just updated, so it's the value from the left. - Longest Common Subsequence keeps two rows:
below(finished) androw(being filled).
When you're learning, write the full table first. Compress it only once the full version works.
How to recognize a 2-D DP problem
- A grid where you move in limited directions (right/down), and you count paths or find a best path.
- Two strings or arrays, and the question is about how they match up: common parts, edits to turn one into the other.
- A 1-D DP where each answer also depends on a second quantity (like "how many items used so far" or "remaining capacity").
Common mistakes
- A fuzzy state definition. Say exactly what
table[i][j]means, including whetheriis "from positioni" or "the firsticharacters". - Forgetting the extra row/column for the empty-string base cases, then indexing past the end.
- Filling in the wrong order, so a cell reads a neighbor that hasn't been computed yet.
- Compressing to one row too early, and overwriting a value you still needed.