Pattern #22
One-Dimensional DP
EssentialDefine dp[i] clearly, then relate it to smaller indices.
When to use
Optimal answer on a prefix, or ways/min cost to reach index i from earlier states only.
Recognition cues
- Climbing stairs / house robber
- Coin change / word break
- LIS (patience or O(n²) DP)
Common pitfalls
- Unclear state definition (what does dp[i] mean?)
- Wrong iteration order for dependencies
- Off-by-one on base cases
90-second recognition drill
Which pattern fits best?
- Climbing stairs / house robber
- Coin change / word break
- LIS (patience or O(n²) DP)
Interactive
Mental model
A full worked walkthrough of the invariant. Pause, scrub the dots, or use ← →. Aim to narrate each step yourself.
dp[0] = 0, else ∞
How to think about it
Name dp[i] in plain language (min coins for amount i; max money through house i). Express it using earlier cells. Fill in an order that respects dependencies. Compress space if only last few cells matter.
Template shapes
| Shape | Core move | Example |
|---|---|---|
| Stairs | ways(i) = ways(i - 1) + ways(i - 2). Two rolling values store the previous step and the one before it |
LC 70 |
| Word break | dp[0] is true. dp[i] is true when some dictionary word ends at i and dp is true at that word’s start. A set tests the slice s[j:i] |
LC 139 |
| House robber | dp[i] = max(dp[i - 1], dp[i - 2] + nums[i]). Skip this house, or rob it after skipping the previous one |
LC 198 |
| Circular robber | The linear robber runs on [0, n - 2] and on [1, n - 1]. The answer is the larger result. One house is nums[0] |
LC 213 |
| LIS | tails holds the smallest tail of each length built so far. Binary search replaces a tail or appends one. The number of tails is the length |
LC 300 |
| Coin change | dp[0] = 0 and every other amount starts unreachable. Each coin relaxes dp[a] = min(dp[a], dp[a - coin] + 1) |
LC 322 |
Complexity baseline
Stairs and the robber are O(n) time and O(1) rolling space. Word break is O(n²) slice checks. LIS with binary search is O(n log n). Coin change is O(amount · coins).
From template to problem
- Name
dp[i]and the base before the loop. Stairs and the robber need two rolling values (LC 70, LC 198). - Word break seeds
dp[0]and tests the slices[j:i]. Membership of that slice is not a constant-time check per word (LC 139). - A circle runs the linear robber on the two ranges that drop one end house. A single house returns
nums[0](LC 213). - LIS stores the smallest tail of each length. The length is the number of tails (LC 300). Coins leave an unreachable amount alone and only relax it from a reachable
a - coin(LC 322).
Template
Same skeleton in TypeScript, Python, and C#. Adapt the invariant; keep the structure.
/** 1D DP template: Coin Change (fewest coins). dp[x] = min coins for amount x. */
export function coinChange(coins: number[], amount: number): number {
const INF = amount + 1;
const dp = new Array<number>(amount + 1).fill(INF);
dp[0] = 0;
for (let x = 1; x <= amount; x++) {
for (const c of coins) {
if (c <= x) dp[x] = Math.min(dp[x]!, dp[x - c]! + 1);
}
}
return dp[amount]! >= INF ? -1 : dp[amount]!;
}
/** 1D DP template: Coin Change (fewest coins). dp[x] = min coins for amount x. */
export function coinChange(coins: number[], amount: number): number {
const INF = amount + 1;
const dp = new Array<number>(amount + 1).fill(INF);
dp[0] = 0;
for (let x = 1; x <= amount; x++) {
for (const c of coins) {
if (c <= x) dp[x] = Math.min(dp[x]!, dp[x - c]! + 1);
}
}
return dp[amount]! >= INF ? -1 : dp[amount]!;
}
- 1#70 Climbing StairsGuideeasy
- 2#139 Word BreakGuidemedium
- 3#198 House RobberGuidemedium
- 4#213 House Robber IIGuidemedium
- 5#300 Longest Increasing SubsequenceGuidemedium
- 6#322 Coin ChangeGuidemedium