Skip to main content
Back to all problems
Beginner guide, Algorithms & data structures

1-D Dynamic Programming

Break a problem into overlapping subproblems and keep their answers in one array.

Dynamic programming (DP) sounds scary, but the idea is simple: break a problem into smaller versions of itself, solve each smaller version once, and save the answer so you never solve it twice.

"1-D" means the saved answers fit in a one-dimensional array: one answer per position, per amount, or per step.

Why saving answers matters

Take Climbing Stairs: how many ways to climb n steps, taking 1 or 2 at a time? Your last move was a 1-step (from step n - 1) or a 2-step (from step n - 2), so:

text
ways(n) = ways(n - 1) + ways(n - 2)

Written as plain recursion, ways(5) calls ways(4) and ways(3); ways(4) calls ways(3) again, and so on. The same questions get asked over and over, and the work doubles with each step: O(2^n).

Loading diagram...

ways(3) appears twice, ways(2) three times... But there are only n different questions. Answer each one once, and it's O(n).

The three things every DP needs

  1. A clear definition of the state. What does one saved answer mean? Write it as a sentence, like "best[i] = the most money you can rob from houses 0 to i". Most DP bugs come from a fuzzy definition.
  2. A recurrence. How do you get one answer from smaller ones? Usually you ask, "what was the last choice?" (the last step, the last coin, the last word) and try every option.
  3. Base cases. The smallest answers you know without computing anything, like "0 coins make amount 0" or "there's 1 way to decode an empty string".

Two ways to write it

StyleHowFeels like
Memoization (top-down)Write the plain recursion, then save each result in a map and return it if it's already there.the recursion you'd write anyway
Tabulation (bottom-up)Fill an array from the base cases up, in an order where everything you need is already filled.a loop over a table

Both are O(n) for Climbing Stairs. Tabulation avoids deep recursion; memoization is often easier to come up with first. Either is fine in an interview.

Saving memory: keep only what you need

If each answer only uses the previous one or two, you don't need the whole array: two variables are enough. Climbing Stairs, House Robber and Decode Ways all do this, so they use O(1) memory:

ts
let twoBack = 1
let oneBack = 1
for (let i = 2; i <= n; i++) {
  ;[twoBack, oneBack] = [oneBack, oneBack + twoBack]
}

Update both at once (or use a temporary variable), so the new value is computed from the old ones.

When greedy isn't enough

A greedy strategy always takes what looks best right now: the biggest coin, the richest house. It's fast, but often wrong:

  • Coin Change with coins [1, 3, 4] and amount 6: greedy takes 4 + 1 + 1 (3 coins), but 3 + 3 is 2.
  • House Robber on [2, 1, 1, 2]: "every other house" gives 3, but the answer is 4.

DP fixes this by trying every option at each step, while saving answers so it stays fast.

How to recognize a 1-D DP problem

  • It asks for a count ("how many ways"), a minimum, a maximum, or yes/no ("can it be done").
  • Making a choice at one position affects what's allowed later.
  • The answer for a position can be built from answers for earlier positions (or smaller amounts).
  • Trying every combination would be exponential.

Common shapes in this category:

ShapeProblems
"1 or 2 steps back"Climbing Stairs, Decode Ways
"take this one or skip it"House Robber, House Robber II
"try every option for the last piece"Coin Change, Word Break
"best that ends here"Maximum Product Subarray, Longest Increasing Subsequence
"grow from a center" (a DP shortcut)Longest Palindromic Substring, Palindromic Substrings

A recipe to follow

  1. Write the brute-force recursion, asking "what was the last choice?".
  2. Notice it repeats the same questions.
  3. Define the state in one sentence, and write the recurrence and base cases.
  4. Fill a table in order (or add a memo).
  5. If you only look one or two steps back, replace the table with variables.

Practice what you learned

The problems go from easier to harder. Try each one on your own and open a hint when you get stuck.