Pattern

1D dynamic programming

A DP whose state is a single prefix index, where each answer is built from a small fixed number of already-computed earlier answers.

What it is

A 1D dynamic program tracks one running quantity per position in a sequence, where the value at position i is computed from the values at a small, fixed set of earlier positions — usually just i-1 and i-2. The whole technique is: figure out what the last "move" could have been (the last step taken, the last character consumed, whether the current element was included or skipped), express the answer at i in terms of the answers at those specific earlier positions, and fill left to right. Because only the last one or two values are ever read, almost every 1D DP collapses from an O(n) array to two or three rolling variables.

When it applies

Tells in the problem

The classic mistake

Code that only carries forward the single most recent value quietly assumes the last move could only ever be of one size — for example, forgetting that a decode step can consume either one digit or two, or that a running product's best next value might come from extending the minimum seen so far rather than the maximum, because a negative number flips their roles. This produces code that passes the examples with only positive numbers or single-length moves and silently breaks the moment the input includes the case the extra state was there to handle. The second common mistake is getting a base case wrong in a way that doesn't cause a crash — an off-by-one in what "zero prior steps" or "empty prefix" should count as — which then propagates through every later value without ever throwing an error. Before coding, write out the last-move cases explicitly and check the base case against the smallest possible input by hand.

Questions to start with

All 6 1d dynamic programming questions

Problem statement, examples and hints are free on every page; the worked approach, solution and tests are in the company guides.

Free starter pack

13 must-tier questions with full solutions

Sign in with an email — no purchase — and read them in the full reader.

No password, no spam. One sign-in email; you can read the pack in the full reader.