</> MAANG.io
coding interview ยท 101

Foundations

Master coding interviews with comprehensive coverage of data structures, algorithms, and problem-solving techniques. Progress from fundamentals to advanced topics with expertly curated content.

0/255 solved 0% complete

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.

flowchart TD A["Walk along one axis"] --> B["Define dp at index i"] B --> C["Build dp i from a few earlier entries"] C --> D["Set the base cases"] D --> E["Keep only the entries you reread"]

๐Ÿ’ก Fun fact: The "counting bits" idea โ€” that the number of 1-bits in i equals the bits in i with its last bit removed, plus that last bit โ€” is so cheap that modern CPUs ship a single popcount instruction 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 first i elements (or the number i), and the recurrence builds dp[i] from a few earlier entries. Master the 4-step method here and every harder DP is a variation.


The 4-step DP method

The 4-step DP method: 1 Subproblem (what does dp[i] mean), 2 Recurrence (how dp[i] uses earlier dp[]), 3 Base cases (the smallest subproblems), 4 Optimize (table to rolling variables, O(1) space)

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

Linear DP family tree: dp[i] from earlier entries branches into Min Cost Climbing Stairs (the intro), Word Break (DP over a string prefix), Rotated Digits (state from a sub-number), and Counting Bits (DP riding on bit structure)

  • 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 prefix s[: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.

Core idea: the number of 1-bits in i is the number of 1-bits in a smaller number you already solved, plus a tiny correction. Shift i right by one (i >> 1) and you reuse dp[i >> 1], adding back the bit you dropped; or clear the lowest set bit (i & (i - 1)) and you reuse dp[i & (i-1)], adding the one bit you cleared. Either way: build big answers from small ones, O(n) total.

Problem, rephrased

You're given a non-negative integer n. For every integer i from 0 to n (inclusive), count how many 1s appear in its binary representation โ€” its popcount โ€” and return all those counts in an array ans, where ans[i] is the popcount of i.

So this isn't "count the bits of one number." It's "count the bits of every number up to n, cheaply, by noticing that consecutive numbers share almost all of their structure." That sharing is what turns a naive O(n log n) into a clean O(n).

i binary popcount ans[i]
0 000 0
1 001 1
2 010 1
3 011 2
4 100 1
5 101 2

For n = 5 the answer is [0, 1, 1, 2, 1, 2]. Read down the popcount column and a rhythm appears: every value is one more than some earlier value. 5 = 101 has the same upper bits as 2 = 10 plus a trailing 1, so ans[5] = ans[2] + 1. That "one more than an earlier answer" is the whole lesson.

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

Sign in to MAANG.io

Use your Google or Microsoft account โ€” no password to remember.

Continue with Google Continue with Microsoft

Please accept the terms above to continue.