Skip to content
playdsa
Preferences

Make yourself comfortable.

Saved on this browser. Your device’s reduced-motion preference is always respected.

Theme
Advanced settings

The Circular Fuel

Learn
Play
Prove
Problem context and objectives
Mission briefing

Puzzle systems engineer · A game mechanic is behaving incorrectly

Reset after a deficit poisons every earlier start: The Circular Fuel.

Brute force may pass the demo but fail when the world fills with players.

How you win

  1. 1Recognize when Greedy gas-station reset matches the clues
  2. 2Keep this true after every move: tank is the surplus from the current candidate start and total is the surplus over every processed station
  3. 3Reach the result within O(n)

Rules and pressure

  • Target cost: O(n)
  • State rule: tank is the surplus from the current candidate start and total is the surplus over every processed station
Lesson 1 of 3

Live algorithm trace

Greedy gas-station reset

Complete execution
1 of 6

Try station 0 as the first candidate start.

1start = 0; tank = total = 0
2gain = gas[i] - cost[i]
3tank += gain; total += gain
4if tank < 0
5start = i + 1; tank = 0
6return start if total >= 0 else -1
start = 0tank = 0total = 0
Truth to preserve / Cost target
Truth to preserve

tank is the surplus from the current candidate start and total is the surplus over every processed station

Cost target

O(n)

If a candidate start accumulates a negative tank at station i, no station inside that failed segment could reach farther. Reset to i+1 while tracking total surplus to decide global feasibility.

Your call · What should guide every step of this algorithm?

Help shape PlayDSA

Something confusing, broken, or missing? Leave a quick note without leaving your lesson.

Please leave out passwords, payment details and other private information.

Page included: /

Sign in to save feedback here, or send it with your email app. Your draft stays here while you sign in.

Open email instead