Linear DP
What is this?
Linear DP is the simplest flavor of dynamic programming: you walk along a single line โ an array, a string, or a number โ and each answer dp[i] is built from one or two answers you already worked out just before it. It is exactly like figuring out the cheapest way to reach step i on a staircase once you know the cheapest way to reach the steps below. Because each small answer is reused many times, you store it once instead of recomputing it.
๐ก Fun fact: The "counting bits" idea โ that the number of 1-bits in
iequals the bits iniwith its last bit removed, plus that last bit โ is so cheap that modern CPUs ship a singlepopcountinstruction to do it in hardware.
๐ The 4 problems in this chapter are free. Sign in with Google or Microsoft to start solving.
Core idea: Dynamic programming is just remembering the answers to overlapping subproblems instead of recomputing them. Linear DP is where you learn the method on a single axis:
dp[i]answers a question about the firstielements (or the numberi), and the recurrence buildsdp[i]from a few earlier entries. Master the 4-step method here and every harder DP is a variation.
The 4-step DP method
Every lesson here is the same recurrence expressed four ways โ brute recursion, memoized top-down, bottom-up table, and rolling variables. They differ only in what they remember and when.
The problems
- Min Cost Climbing Stairs โ the gentle intro:
dp[i] = cost[i] + min(dp[i-1], dp[i-2]); the place to learn the whole method. - Word Break โ
dp[i]= "is the prefixs[:i]segmentable?"; break at the last word. - Rotated Digits โ a number's verdict composes from
dp[i//10]plus its last digit. - Counting Bits โ
dp[i] = dp[i>>1] + (i&1): every number reuses a smaller solved one.
Key takeaways
- DP = overlapping subproblems + optimal substructure โ remember, don't recompute.
- The recurrence is the whole game; the four implementations just trade memory for clarity.
dp[i]over a prefix is the linear template โ a string, an array, or a number.- Optimize last โ once the table works, keep only the few entries the recurrence reads.
- Why interviewers love it: it reveals whether you can define a subproblem and a transition, the core DP skill.
Start here: Min Cost Climbing Stairs (learn the method), then Word Break.
Min Cost Climbing Stairs
Core idea: Dynamic programming is just remembering the answers to overlapping subproblems instead of recomputing them. The cheapest way to reach step
ionly depends on the two steps you could have jumped from โ so once you know the cost to reach every earlier step, the cost to reachiis one tiny addition. Solve each step once, reuse it forever.
This is your first dynamic-programming lesson, so we're going to go slow and build the method, not just the answer. By the end you'll have a 4-step recipe you can point at almost any "1D DP" problem.
Problem, rephrased
You're at the bottom of a staircase. Each stair i charges you a toll cost[i] the moment you step onto it. From any stair you may climb 1 step or 2 steps. You're allowed to start โ for free โ on either stair 0 or stair 1. Your goal is to reach the top of the staircase, which is the floor one past the last stair (it has no toll).
Minimize the total tolls you pay.
cost = [10, 15, 20]
stair: 0 1 2 (top)
toll: 10 15 20 0
^ ^ ^
start start here reach here, past the last stair
Start on stair 1 (pay 15), jump 2 to the top (past stair 2). Total = 15. That beats starting on stair 0.
Inputs / outputs
cost |
best plan | answer |
|---|---|---|
[10, 15, 20] |
start at 1 (15), jump 2 to top | 15 |
[1,100,1,1,1,100,1,1,100,1] |
hop the cheap 1's, skip the 100's | 6 |
[0, 0] |
start at 0, jump 2 to top (or start at 1) | 0 |
[5, 10] |
start at 1 (10)? no โ start at 0 (5), jump 2 | 5 |
You pay for every stair you land on; you never pay for the top because there's no stair there.
Sign in to continue reading
The rest of this lesson is available with a free account. Signing in with Google or Microsoft is free.
Sign in to read the full lesson