ntree.ai
ntree.aiMinimum Number of Refueling Stops

Minimum Number of Refueling Stops

hardsolvedattempted

A car drives east from position 0 to position target. It starts with startFuel litres in a tank of unlimited size and burns one litre per mile.

Along the way there are gas stations: stations[i] = [position, fuel] means a station position miles from the start that holds fuel litres. When the car stops at a station it takes all of that station's fuel. The stations are listed in increasing order of position.

Return the fewest stops needed to reach target, or -1 if it cannot be reached. Arriving anywhere with an empty tank still counts as arriving.

Example 1:

Input:  target = 100, startFuel = 1, stations = [[10, 100]]
Output: -1
Explanation: the car runs out after 1 mile, before the first station.

Example 2:

Input:  target = 100, startFuel = 10,
        stations = [[10, 60], [20, 30], [30, 30], [60, 40]]
Output: 2
Explanation: stop at mile 10 for 60 litres and at mile 60 for 40 more.

Constraints:

  • 1 <= target, startFuel <= 10^9
  • 0 <= length of stations <= 500
  • 1 <= position < target for every station, in increasing order
  • 1 <= fuel <= 10^9
def minRefuelStops(target: int, startFuel: int, stations: list[list[int]]) -> int:
pass
 

Press Visualise to step through your code.