871. Minimum Number of Refueling Stops
871. Minimum Number of Refueling Stops
Description
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] = [position<sub>i</sub>, fuel<sub>i</sub>]
indicates that the i^th
gas station is position<sub>i</sub>
miles east of the starting position and has fuel<sub>i</sub>
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:
1 | Input: target = 1, startFuel = 1, stations = [] |
Example 2:
1 | Input: target = 100, startFuel = 1, stations = [[10,100]] |
Example 3:
1 | Input: target = 100, startFuel = 10, stations = [[10,60],[20,30],[30,30],[60,40]] |
Constraints:
1 <= target, startFuel <= 10^9
0 <= stations.length <= 500
- 1 <= positioni < positioni+1 < target
- 1 <= fueli < 10^9
Hints/Notes
- 2025/02/08 Q3
- priority queue
- 0x3Fâs solution
Solution
Language: C++
1 | class Solution { |