For [2,7,9,3,1], what must a state remember to decide whether to take the final house?
Name a subproblem, not a variable.
1 / 13
Climbing Stairs — predict before coding
Partition routes by their final move. A route ending in one step comes from n-1; one ending in two steps comes from n-2. These alternatives are disjoint and exhaustive.
Decision: List the five ordered routes for n=4. Why are 1,2,1 and 2,1,1 different routes?
2 / 13
Worked trace — compute before shifting
House values [2,7,9,3,1]; both prefix states begin at 0.
Value
Old two-back
Old one-back
Take
New optimum
2
0
0
2
2
7
0
2
7
7
9
2
7
11
11
3
7
11
10
11
1
11
11
12
12
3 / 13
Faded trace — finish, then diagnose
House values [4,1,2,7]; first two prefix optima: 4,4.
Before value 2: (two-back,one-back)=(4,4).
Complete the remaining (skip,take,new optimum) rows.
What fails if two-back = one-back executes before computing take?
4 / 13
House Robber — predict before coding
The final house is either skipped or taken. Taking it forbids exactly the previous house, so only two optimal prefix values are needed—not a remembered list of greedy choices.
Decision: For [3,2,5,10,7], reconstruct the prefix optima without looking at the worked trace. Which indices attain the answer?
5 / 13
House Robber II — predict before coding
The extra circular edge only forbids choosing both endpoints. Every valid selection therefore belongs to at least one of two linear problems: exclude the first or exclude the last.
Decision: Why does [2,3,2] yield 3 instead of 4? Why is a singleton [7] not reduced to an empty line?
6 / 13
Maximum Subarray — predict before coding
The useful state is the best nonempty subarray ending exactly here, not the best subarray anywhere. A negative preceding sum can only hurt a segment that includes the current value.
Decision: For [-5,-2,-8], what is the answer and why is initializing the answer to zero incorrect?
7 / 13
Maximum Product Subarray — predict before coding
Multiplication can reverse ordering: a very negative product becomes very positive after another negative factor. Retain both extreme products ending at the previous index.
Decision: For [-2,4,-3], the first two values leave (oldMax,oldMin)=(4,-8). Compute the three candidates for the next value. A buggy direct recurrence writes maxEnding first, then uses that new value to compute minEnding. Which impossible minimum can it produce?
8 / 13
Word Break — predict before coding
A dictionary word is an edge between two prefix boundaries. The task is reachability from boundary 0 to boundary len(s), not greedy longest-word selection.
Decision: For s="aaaa" and dictionary ["a","aa"], change the output from existence to the number of segmentations. What state replaces a boolean, why must the predecessor loop keep going, and what is the result?
9 / 13
Coin Change — predict before coding
Amounts form a directed acyclic graph when every coin is positive: taking a coin moves from a smaller amount to a larger one. The desired value is a shortest path measured in coin count.
Decision: With coins [2,3], complete the minimum-count table for amounts 0..7. Use unreachable where needed. Then change the task: each listed coin may be used at most once. Why would the existing recurrence give a wrong answer for coins [3], amount 6?
10 / 13
Longest Increasing Subsequence — predict before coding
For a fixed subsequence length, only its smallest achievable ending value matters for future extensions. This dominance rule compresses quadratic endpoint DP into a sorted array of tails.
Decision: Process [3,5,6,1,2,4] with tails. Give its final array and length. Which length-three witness does it certify, and why can the old ending value 6 be discarded when 4 arrives?
11 / 13
Decode Ways — predict before coding
Every valid decoding ends in either one valid nonzero digit or one valid two-digit number from 10 through 26. Zero cannot form a letter by itself.
Decision: Compute the counts for 1120 and 2101 by separating the final one-digit and two-digit transitions. A shortcut rejects every string containing zero: which valid final choices does that throw away?