Free beta: 60 days of full access, no card needed.84 seats leftSign up free

We use necessary cookies to run the site (sign-in and language). If you accept, we also load Google Analytics to see which pages are used, and Google reCAPTCHA to keep spam off the contact and bug-report forms. Privacy policy

All patterns

Dynamic programming (1-D)

O(states)

Answer a small version of the problem, store the result, then build the next one from that. Every state is computed once.

Updated Oct 17, 2026

How does Dynamic programming (1-D) work?

Decide what one state means. Usually it is the best answer over the first i items.

Write the transition: how state i follows from earlier ones. That transition is the whole algorithm.

Fill the base cases by hand. Those are the states with nothing smaller to lean on.

Loop forward and fill the table in order. Every state it reads is already finished.

The answer is one cell, usually the last. Sometimes it is the largest cell in the table.

If the transition looks only two states back, keep two variables. The array is then unnecessary.

Sources: Wikipedia: Dynamic programming, Bellman (1954), The theory of dynamic programming

  1. dp[0] = 2Robbing houses [2, 7, 9]. Taking only the first gives 2.
  2. dp[1] = 7Take the better of 2 and 7. Neighbouring houses cannot both be robbed.
  3. dp[2] = max(7, 2 + 9)Either skip the third house, or take it plus dp[0].
  4. dp[2] = 11Taking the first and third houses wins.
  5. answer = 11Three states, each computed exactly once.
Largest sum of a contiguous stretch
maxSubarraySum([-2, 1, -3, 4, -1, 2, 1, -5])
nums
-2
1
-3
4
-1
2
1
-5
curr at each i: the best sum of a run that ends at i
curr = -2best = -2

Find the largest sum of a contiguous stretch. The state at i is the best sum of a run that ends exactly at i.

Speed race
Plain recursion and a bottom-up table on the same 18 stairs

Bars: how many times the answer for each stair i was computed, i from 0 to 18.

One operation is one call or one table cell. Every frame, each lane does the same number of operations, so the slow one falls behind.

Plain recursion O(2ⁿ)
0 operations
Bottom-up table O(n)
0 operations

 

And with more numbers

Operations for n numbers, and roughly how long they take at 100 million simple operations a second. An estimate from the same count, not a measurement.

n = 20
Plain recursion21.9K · under 1 ms
Bottom-up table21 · under 1 ms
n = 40
Plain recursion331.2M · 3.3 s
Bottom-up table41 · under 1 ms
n = 60
Plain recursion5T · 14 h
Bottom-up table61 · under 1 ms

The Dynamic programming (1-D) code template

function maxSubarraySum(nums) {
    let best = nums[0];
    let curr = nums[0];
    for (let i = 1; i < nums.length; i++) {
        curr = Math.max(nums[i], curr + nums[i]);
        best = Math.max(best, curr);
    }
    return best;
}

A worked example of Dynamic programming (1-D)

Largest sum of a contiguous stretch

Return the largest sum of any contiguous stretch of an array.

Values can be negative, so the answer is not simply the total.

Let state i be the best sum of a stretch that ends at index i.

Either the previous stretch continues, or a new one starts here. The answer is the largest state.

function maxSubArray(nums) {
    let best = nums[0];
    let endingHere = nums[0]; // only the previous state matters, so no array

    for (let i = 1; i < nums.length; i++) {
        // continue the stretch, or start a new one right here
        endingHere = Math.max(nums[i], endingHere + nums[i]);
        best = Math.max(best, endingHere);
    }

    return best;
}

When should you use Dynamic programming (1-D)?

Use Dynamic programming (1-D) when the problem statement says “best/maximum/minimum ending at position i”, “ways to reach, climb, or tile” or “cannot pick two adjacent items”. A solution built on it usually runs in O(states).

  • best/maximum/minimum ending at position i
  • ways to reach, climb, or tile
  • cannot pick two adjacent items
  • minimum cost path through a grid
  • overlapping subproblems, same state recomputed

The signal words of every pattern sit in one table, the algorithm patterns cheat sheet.

What is Dynamic programming (1-D) confused with?

Dynamic programming (1-D) is most often confused with Recursion with memoization, Greedy (exchange argument), Recursion and DP on subsequences (LIS / LCS / edit distance).

Common mistakes with Dynamic programming (1-D)

  • A state that does not decide the future

    If two situations share a state but behave differently, the state is incomplete. Add what is missing.

  • Filling the table in the wrong order

    A cell has to be written before anything reads it. Follow the direction the transition needs.

  • Reading the wrong cell as the answer

    For a best-ending-here state the answer is the maximum. It is not the last cell.

  • Forgetting the base cases

    An empty or single-element input is where most of these break. Handle those first.

Interview problems solved with Dynamic programming (1-D) include Climbing stairs, House robber, Maximum subarray, Coin change, Decode ways, Min cost climbing stairs and Fibonacci.

  • Climbing stairs: Each step comes from one or two behind.
  • House robber: Take this house plus two back, or skip it.
  • Maximum subarray: Continue the stretch or start a new one.
  • Coin change: The state is the amount, and every coin is a branch.
  • Decode ways: One digit or two, when the pair is valid.
  • Min cost climbing stairs: The same shape with a cost on each step.
  • Fibonacci: The shortest transition there is.

What is the time and space complexity of Dynamic programming (1-D)?

The target complexity of Dynamic programming (1-D) is O(states).

n states with O(1) work each gives O(n). Memory drops to O(1) when only the last states matter.

See where this fits in the 150-step track