Problems > Fewest Refuels > Editorial

Fewest Refuels — Solution & Editorial

Back to the Problem

Greedy with a max-heap: drive as far as fuel allows, banking every reachable station's fuel in a heap; when stranded, refuel from the largest banked station. Each refuel is one stop.

Complexity: O(N log N)

Watch out: You may reach a station and choose NOT to buy there — bank it in the heap and buy later only if needed.