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.
Input: target =1, startFuel =1, stations =[]Output: 0Explanation: We can reach the target without refueling.
Example 2:
1
2
3
Input: target =100, startFuel =1, stations =[[10,100]]Output: -1Explanation: We can not reach the target(or even the first gas station).
Example 3:
1
2
3
4
5
6
7
Input: target =100, startFuel =10, stations =[[10,60],[20,30],[30,30],[60,40]]Output: 2Explanation: We start with10 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 return2.
The key idea is to always refuel at the station with the most fuel among all stations we’ve passed but haven’t used yet. This greedy approach ensures we maximize our progress at every refueling stop, minimizing the total number of stops.
Use a max heap to keep track of the fuel amounts at stations we’ve passed.
Start with the initial fuel and iterate through each station (and finally the target).
For each station:
Subtract the distance from the previous station (or start) from the current fuel.
If the fuel is insufficient to reach the current station, refuel from the largest available station(s) in the heap until we can reach or the heap is empty.
If still unable to reach, return -1.
Add the current station’s fuel to the heap.
After all stations, repeat the process to reach the target.
classSolution {
public:int minRefuelStops(int tgt, int fuel, vector<vector<int>>& st) {
priority_queue<int> pq;
int ans =0, prev =0, i =0, n = st.size();
while (fuel < tgt) {
while (i < n && st[i][0] <= fuel) pq.push(st[i++][1]);
if (pq.empty()) return-1;
fuel += pq.top(); pq.pop();
ans++;
}
return ans;
}
};
classSolution {
publicintminRefuelStops(int tgt, int fuel, int[][] st) {
PriorityQueue<Integer> pq =new PriorityQueue<>(Collections.reverseOrder());
int ans = 0, i = 0, n = st.length;
while (fuel < tgt) {
while (i < n && st[i][0]<= fuel) pq.offer(st[i++][1]);
if (pq.isEmpty()) return-1;
fuel += pq.poll();
ans++;
}
return ans;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
classSolution {
funminRefuelStops(tgt: Int, fuel: Int, st: Array<IntArray>): Int {
val pq = java.util.PriorityQueue<Int>(compareByDescending { it })
var ans = 0var i = 0val n = st.size
var currFuel = fuel
while (currFuel < tgt) {
while (i < n && st[i][0] <= currFuel) pq.add(st[i++][1])
if (pq.isEmpty()) return -1 currFuel += pq.poll()
ans++ }
return ans
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
classSolution:
defminRefuelStops(self, tgt: int, fuel: int, st: list[list[int]]) -> int:
import heapq
pq: list[int] = []
ans = i =0 n = len(st)
while fuel < tgt:
while i < n and st[i][0] <= fuel:
heapq.heappush(pq, -st[i][1])
i +=1ifnot pq:
return-1 fuel +=-heapq.heappop(pq)
ans +=1return ans
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
impl Solution {
pubfnmin_refuel_stops(tgt: i32, mut fuel: i32, st: Vec<Vec<i32>>) -> i32 {
use std::collections::BinaryHeap;
letmut pq = BinaryHeap::new();
letmut ans =0;
letmut i =0;
let n = st.len();
while fuel < tgt {
while i < n && st[i][0] <= fuel {
pq.push(st[i][1]);
i +=1;
}
if pq.is_empty() { return-1; }
fuel += pq.pop().unwrap();
ans +=1;
}
ans
}
}