871. 最低加油次数
汽车从起点出发驶向目的地,该目的地位于出发位置东面 target 英里处。
沿途有加油站,每个 station[i] 代表一个加油站,它位于出发位置东面 station[i][0] 英里处,并且有 station[i][1] 升汽油。
假设汽车油箱的容量是无限的,其中最初有 startFuel 升燃料。它每行驶 1 英里就会用掉 1 升汽油。
当汽车到达加油站时,它可能停下来加油,将所有汽油从加油站转移到汽车中。
为了到达目的地,汽车所必要的最低加油次数是多少?如果无法到达目的地,则返回 -1 。
注意:如果汽车到达加油站时剩余燃料为 0,它仍然可以在那里加油。如果汽车到达目的地时剩余燃料为 0,仍然认为它已经到达目的地。
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/minimum-number-of-refueling-stops
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
import java.util.Arrays;
import java.util.Comparator;
import java.util.PriorityQueue;
class Solution {
public int minRefuelStops(int target, int startFuel, int[][] stations) {
Arrays.sort(stations, new Comparator<int[]>() {
@Override
public int compare(int[] o1, int[] o2) {
return Integer.compare(o1[0], o2[0]);
}
});
PriorityQueue<Integer> queue = new PriorityQueue<>(new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
return Integer.compare(o2, o1);
}
});
int ans = 0;
int fuel = startFuel;
int position = 0;
for (int i = 0; i < stations.length && position < target; ++i) {
int distance = stations[i][0] - position;
while (fuel < distance && !queue.isEmpty()) {
ans++;
fuel += queue.poll();
}
if (fuel < distance) {
return -1;
}
fuel -= distance;
position = stations[i][0];
queue.offer(stations[i][1]);
}
position += fuel;
while (position < target && !queue.isEmpty()) {
ans++;
position += queue.poll();
}
return position >= target ? ans : -1;
}
}
心之所向,素履以往 生如逆旅,一苇以航

浙公网安备 33010602011771号