Skip to the workspace
Pebellslearn by building
Planning DP  ›  Lesson

Making a DP Faster & the Refuel Plan

DP Optimisation 34 minWhen the inner loop takes a min over a sliding window, a deque answers it in O(1)

dsa / Making a DP Faster & the Refuel Plan

Explore each scene at your own pace.

01 · Where the time goes

Where the time goes

Making a DP Faster & the Refuel Plan

Definition
DP optimisation speeds up a recurrence by avoiding repeated work in its transition; for a sliding-window minimum, a monotonic deque keeps only the candidates that can still win, so each step costs constant amortised time instead of scanning the whole window.
In plain words
Picture a queue of fuel stops where a new, cheaper stop makes every older, dearer one pointless, so they leave the queue for good. The cheapest option is always at the front, and nobody has to re-check the whole line.

In short: a DP transition that scans a sliding window for a minimum (cost[i] = fees[i] + min(cost[i − reach..i − 1])) does the same comparisons over and over, because consecutive windows mostly overlap. A monotonic deque fixes it by keeping only candidates that could still win: anything older and no cheaper than a newer arrival can never be the answer again, so it's evicted for good. What's left increases front to back, the cheapest always at the front, and every station is pushed once and popped at most once — O(1) amortised per station instead of O(reach).

Eight stations' deque after the sweep: boxes 0,2,5,7 kept with increasing costs 4,7,9,14 and boxes 1,3,4,6 crossed out as evicted, each with an arrow to the cheaper station that evicted it

Meridian's long-haul van crosses the region on a road lined with fuel stations. Each one charges a different stop fee, and the tank only reaches so far, so consecutive stops can be at most reach stations apart. The van has to stop at the first station and the last. Which stations should it use?

You already know how to write this DP:

cost[i] = fees[i] + min(cost[i − reach], …, cost[i − 1])

It's correct, and it scans up to reach cells for every station. On a route with 200,000 stations and a reach of 500 that's 100 million reads for one plan, and dispatch wants to re-plan every time a price changes. This lesson doesn't find a new DP. It takes a DP you already have and makes the inner loop disappear.

Where the time goes

Count the reads the scan makes on a short route:

What does this print?
reads = 0
n, reach = 10, 3
for i in range(1, n):
    for j in range(max(0, i - reach), i):
        reads += 1
print(reads)

Look at what consecutive stations actually scan. Station 5 looks at 2, 3, 4. Station 6 looks at 3, 4, 5. The window slides forward by one: one station leaves at the back, one arrives at the front, and the other reach − 1 are the same stations as last time. The scan throws away everything it learned and starts again, every time.

Scene 1 of 8
Project context and further resources
You're building a piece ofMeridian — the day planner
This piece — cheapest_refuels(): Chooses fuel stops along a route in linear time with a sliding-window minimum.
Scenario Meridian's long-haul van crosses the region on a route lined with fuel stations, each charging a different stop fee, and its tank only reaches so many stations ahead. Planning a route with thousands of stations needs to be instant, so the planner can't rescan every station in reach for every stop.
cheapest_refuels.py
Call the function you wrote — just like your app's frontend would. Edit the inputs and hit Call.
cheapest_refuels(fees, reach) → dictThe cheapest set of fuel stops: the total fee and the station numbers, first to last.
try:
returned
Press Run to execute your code.
Press Check to run the tests for this lesson.
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.