A car travels from a starting position to a destination which is target
miles east of the starting position.
There are gas stations along the way. The gas stations are represented as an array stations
where stations[i] = [positioni, fueli]
indicates that the ith
gas station is positioni
miles east of the starting position and has fueli
liters of gas.
The car starts with an infinite tank of gas, which initially has startFuel
liters of fuel in it. It uses one liter of gas per one mile that it drives. When the car reaches a gas station, it may stop and refuel, transferring all the gas from the station into the car.
Return the minimum number of refueling stops the car must make in order to reach its destination. If it cannot reach the destination, return -1
.
Note that if the car reaches a gas station with 0
fuel left, the car can still refuel there. If the car reaches the destination with 0
fuel left, it is still considered to have arrived.
Example 1:
Input: target = 1, startFuel = 1, stations = [] Output: 0 Explanation: We can reach the target without refueling.
Example 2:
Input: target = 100, startFuel = 1, stations = [[10,100]] Output: -1 Explanation: We can not reach the target (or even the first gas station).
Example 3:
Input: target = 100, startFuel = 10, stations = [[10,60],[20,30],[30,30],[60,40]] Output: 2 Explanation: We start with 10 liters of fuel. We drive to position 10, expending 10 liters of fuel. We refuel from 0 liters to 60 liters of gas. Then, we drive from position 10 to position 60 (expending 50 liters of fuel), and refuel from 10 liters to 50 liters of gas. We then drive to and reach the target. We made 2 refueling stops along the way, so we return 2.
Constraints:
1 <= target, startFuel <= 109
0 <= stations.length <= 500
1 <= positioni < positioni+1 < target
1 <= fueli < 109
When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force approach to finding the fewest refueling stops involves trying every possible combination of gas stations. We essentially explore every possible path, checking if it leads to our destination, and then pick the shortest path, i.e., the path with the fewest stops.
Here's how the algorithm would work step-by-step:
def min_refuel_stops_brute_force(target_distance, start_fuel, stations):
number_of_stations = len(stations)
# Iterate through all possible numbers of refueling stops, starting from 0
for number_of_stops in range(number_of_stations + 1):
# Iterate through all possible combinations of gas stations for the current number of stops
for combination in combinations(range(number_of_stations), number_of_stops):
# Check if the combination of stations allows reaching the target
if can_reach_destination(target_distance, start_fuel, stations, combination):
return number_of_stops
return -1
from itertools import combinations
def can_reach_destination(target_distance, start_fuel, stations, combination):
current_fuel = start_fuel
current_position = 0
stations_visited = sorted([stations[i] for i in combination])
# Iterate through the stations and check if we can reach each one
for station_index in range(len(stations_visited)):
distance_to_station = stations_visited[station_index][0] - current_position
# We can't reach this station with our current fuel, this path is invalid
if current_fuel < distance_to_station:
return False
current_fuel -= distance_to_station
current_position = stations_visited[station_index][0]
current_fuel += stations_visited[station_index][1]
# Check if we can reach the destination from the last station
distance_to_target = target_distance - current_position
if current_fuel >= distance_to_target:
return True
else:
return False
# Testing
# target_distance = 1000
# start_fuel = 100
# stations = [[100, 200], [200, 400], [300, 600], [400, 800], [600, 500]]
# result = min_refuel_stops_brute_force(target_distance, start_fuel, stations)
# print(result)
# target_distance = 100
# start_fuel = 10
# stations = [[10,60],[20,30],[30,30],[60,40]]
# result = min_refuel_stops_brute_force(target_distance, start_fuel, stations)
# print(result)
The best way to solve this problem is to think about it step-by-step, always making the smartest decision at each gas station. The key is to prioritize refueling at stations that let us travel the furthest.
Here's how the algorithm would work step-by-step:
def min_refuel_stops(target, start_fuel, stations):
number_of_refuels = 0
current_fuel_reach = start_fuel
stations_visited = []
stations.append((target, 0))
for station_distance, station_fuel in stations:
# If we can't reach the next station, refuel.
while current_fuel_reach < station_distance:
if not stations_visited:
return -1
# Always refuel at the best station seen so far.
best_station_fuel = max(stations_visited)
current_fuel_reach += best_station_fuel
stations_visited.remove(best_station_fuel)
number_of_refuels += 1
stations_visited.append(station_fuel)
return number_of_refuels
Case | How to Handle |
---|---|
Empty stations array. | If the stations array is empty and startFuel is enough to reach target, return 0; otherwise, return -1. |
Initial fuel is already sufficient to reach the target without refueling. | Return 0 immediately as no refueling is needed. |
It's impossible to reach the target even with refueling at all stations. | Return -1 when, after exhausting all possible refueling stops, we cannot reach the target. |
A station has zero fuel. | Treat a station with zero fuel as a valid station if it's reachable, but it doesn't contribute any fuel to the tank. |
Stations are very close to the starting point but far from the destination, requiring many small refills initially. | The solution should correctly prioritize nearer stations with higher fuel capacity to minimize refills. |
Stations are sorted in reverse order of distance from the starting point. | The algorithm should still correctly find the minimum number of refueling stops even with reverse sorted stations. |
Integer overflow when calculating the current fuel level. | Use long data type to prevent potential overflow during fuel calculations. |
Large number of stations, potentially exceeding memory limits. | An efficient implementation, possibly using a priority queue, should handle a large number of stations without running out of memory. |