This is a Greedy problem. We drive as far as we can with our current fuel. Along the way, we pass gas stations. We store the fuel available at these stations in a Max-Heap. If we run out of fuel before reaching the next station or the target, we pop the largest fuel amount from our heap (as if we had stopped at that station earlier).
- Initialize a Max-Heap for station fuel.
- Initialize
curr_dist = startFuel,stops = 0, andi = 0(station index). - While
curr_dist < target:- Add all stations that we have already passed or reached (
stations[i][0] <= curr_dist) to the heap. - If the heap is empty, we cannot go further. Return -1.
- Pop the largest fuel, add it to
curr_dist, and incrementstops.
- Add all stations that we have already passed or reached (
- Return
stops.
- Time Complexity: O(N log N).
- Space Complexity: O(N).
import heapq
def min_refuel_stops(target, startFuel, stations):
# Max-heap for fuel amounts
h = []
res = 0
curr = startFuel
i = 0
while curr < target:
# Add all reachable stations to the heap
while i < len(stations) and stations[i][0] <= curr:
heapq.heappush(h, -stations[i][1])
i += 1
if not h:
return -1
# Refuel from the best station passed so far
curr += -heapq.heappop(h)
res += 1
return res