All topics
111

Filling a table where each answer leans on the last one

Tier 1Locked

Where it runs

1. The problem

Greedy goes wrong when the best choice right now spoils the best overall result. Dynamic programming helps then. You split the task into small subtasks that share work. You solve each one once and later take its answer ready-made.

You have a row of daily numbers: the money made each day. Some days had refunds, so the number goes below zero. Finance asks: which few days in a row made the most money together? The answer can be a single day or the whole month, but usually it is a stretch somewhere in between.

The slow way: try every start and every end, and add up every stretch. On one month that is fine. On years of data it is the O(n2) trap you learned to spot back at lesson 09.

You can answer this question in one pass. The method that gives it makes many scary-looking problems simple. That method is the topic of this lesson.

This step is part of the paid track

The first 33 steps are open to everyone: lesson, exercise and tests. This one opens with full access. Start with a free account and the free steps.

Create a free accountSee what is included
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

Recognise it

Dynamic programming (1-D)

Say these words and it is this pattern

  • 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

Looks similar, isn't

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

On LeetCode

Problems where the best answer at position i depends only on the answers one or two positions back, such as robbing houses, climbing stairs or the best run ending here. Two running numbers replace the whole table.

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