At a rolled grid cell, why does dp[c] mean “above” while dp[c-1] means “left”?
Label old and new values before updating.
1 / 11
Unique Paths — predict before coding
Classify paths by their final move into a cell. Right/down movement makes the grid acyclic, so path counts can be accumulated in row-major order.
Decision: A 2-by-4 grid permits right/down moves. Count its paths, then allow a diagonal down-right move. Derive the new second row and the additional rolled dependency.
2 / 11
Unique Paths II — predict before coding
An obstacle removes a vertex from the path graph. Its count must become zero immediately so it cannot leak an earlier row’s paths into the current row.
Decision: For obstacle rows 0100 / 0000 / 0010, the first rolled row is [1,0,0,0]. Complete the next two rows. A buggy obstacle branch says continue without writing zero; where does its first incorrect count appear?
3 / 11
Minimum Path Sum — predict before coding
The set of predecessors is identical to path counting, but the aggregation changes: choose the cheaper predecessor, then pay for the current cell exactly once.
Decision: For [[1,3,1],[1,5,1],[4,2,1]], what is the minimum path sum and which path achieves it?
4 / 11
Triangle — predict before coding
Choose a state with the direction of dependency made explicit: the minimum cost from a position down to the bottom. Then the already-computed row below summarizes every possible suffix.
Decision: For triangle [[2],[3,4],[6,5,7],[4,1,8,3]], the first bottom-up update gives [7,6,10]. Finish the remaining two updates. A learner aliases the final input row as workspace: does a correct numeric answer establish that the input was preserved?
5 / 11
Worked trace — save the old diagonal
MinDistance("ab", "ac"): old row [1,0,1].
Save diagonal 1, then write boundary dp[0]=2.
Cell
Above
Left
Diagonal
Write
Next diagonal
b / a
0
2
1
1
0
b / c
1
1
0
1
1
Final row: [2,1,1]. One replacement suffices.
6 / 11
Faded trace — a match is a predecessor
MinDistance("ab", "ba"): old row [1,1,1].
Save diagonal 1; write dp[0]=2.
Column 1 matches b with b: write 1, carry old above 1.
Finish column 2 and the row. Is swapping adjacent bytes one allowed edit?
7 / 11
Edit Distance — predict before coding
A state compares two complete prefixes. Classify the last alignment step as a match, replacement, deletion, or insertion; each action removes at least one final byte from consideration.
Decision: For MinDistance("aa", "a"), row 1 is [1,0]. Complete row 2 and explain why a matching final byte does not make the distance zero. What boundary value must be saved before dp[0] is changed?
8 / 11
Interleaving String — predict before coding
A state records how many bytes have been consumed from each source. Their sum fixes the target position, so no third DP dimension is needed.
Decision: For s1="ab", s2="cd", compare targets "acbd" and "adbc". They have identical byte counts. Which is an interleaving, and at which target prefix does the other become impossible?
9 / 11
Maximal Square — predict before coding
A square ending at the current cell can grow only as far as all three neighboring squares support it: above, left, and diagonal. The weakest neighbor limits the side length.
Decision: For byte rows 01 / 11, fill the square-side table. At the final cell, above and left both support side 1, but the old diagonal is zero. What goes wrong if the recurrence ignores the diagonal?
10 / 11
Reconstruct and transfer
State each cell’s meaning, boundary values, predecessor guards, and update direction.
Return the requested count, cost, boolean, or area—not the internal state by accident.
11 / 11
←/→ or click edges to navigate · ? help · N notes · F fullscreen