LC 871. 最低加油次数

题目描述

这是 LeetCode 上的 871. 最低加油次数 ,难度为 困难

汽车从起点出发驶向目的地,该目的地位于出发位置东面 target 英里处。

沿途有加油站,每个 station[i] 代表一个加油站,它位于出发位置东面 station[i][0] 英里处,并且有 station[i][1] 升汽油。

假设汽车油箱的容量是无限的,其中最初有 startFuel 升燃料。它每行驶 $1$ 英里就会用掉 $1$ 升汽油。

当汽车到达加油站时,它可能停下来加油,将所有汽油从加油站转移到汽车中。

为了到达目的地,汽车所必要的最低加油次数是多少?如果无法到达目的地,则返回 $-1$ 。

注意:如果汽车到达加油站时剩余燃料为 $0$,它仍然可以在那里加油。如果汽车到达目的地时剩余燃料为 $0$,仍然认为它已经到达目的地。

示例 1:

1
2
3
4
5
输入:target = 1, startFuel = 1, stations = []

输出:0

解释:我们可以在不加油的情况下到达目的地。

示例 2:
1
2
3
4
5
输入:target = 100, startFuel = 1, stations = [[10,100]]

输出:-1

解释:我们无法抵达目的地,甚至无法到达第一个加油站。

示例 3:
1
2
3
4
5
6
7
8
9
10
输入:target = 100, startFuel = 10, stations = [[10,60],[20,30],[30,30],[60,40]]

输出:2

解释:
我们出发时有 10 升燃料。
我们开车来到距起点 10 英里处的加油站,消耗 10 升燃料。将汽油从 0 升加到 60 升。
然后,我们从 10 英里处的加油站开到 60 英里处的加油站(消耗 50 升燃料),
并将汽油从 10 升加到 50 升。然后我们开车抵达目的地。
我们沿途在1两个加油站停靠,所以返回 2

提示:

  • $1 <= target, startFuel, stations[i][1] <= 10^9$
  • $0 <= stations.length <= 500$
  • $1 <= position{i} < position{i+1} < target$
  • $1 <= fuel_{i} < 10^9$

贪心 + 优先队列(堆)

我们可以模拟行进过程,使用 n 代表加油站总个数,idx 代表经过的加油站下标,使用 remain 代表当前有多少油(起始有 remain = startFuel),loc 代表走了多远,ans 代表答案(至少需要的加油次数)。

只要 loc < target,代表还没到达(经过)目标位置,我们可以继续模拟行进过程。

每次将 remain 累加到 loc 中,含义为使用完剩余的油量,可以去到的最远距离,同时将所在位置 stations[idx][0] <= loc 的加油站数量加入优先队列(大根堆,根据油量排倒序)中。

再次检查是否满足 loc < target(下次循环),此时由于清空了剩余油量 remain,我们尝试从优先队列(大根堆)中取出过往油量最大的加油站并进行加油(同时对加油次数 ans 进行加一操作)。

使用新的剩余油量 remain 重复上述过程,直到满足 loc >= target 或无油可加。

容易证明该做法的正确性:同样是消耗一次加油次数,始终选择油量最大的加油站进行加油,可以确保不存在更优的结果。

Java 代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Solution {
public int minRefuelStops(int target, int startFuel, int[][] stations) {
PriorityQueue<Integer> q = new PriorityQueue<>((a,b)->b-a);
int n = stations.length, idx = 0;
int remain = startFuel, loc = 0, ans = 0;
while (loc < target) {
if (remain == 0) {
if (!q.isEmpty() && ++ans >= 0) remain += q.poll();
else return -1;
}
loc += remain; remain = 0;
while (idx < n && stations[idx][0] <= loc) q.add(stations[idx++][1]);
}
return ans;
}
}

C++ 代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class Solution {
public:
int minRefuelStops(int target, int startFuel, vector<vector<int>>& stations) {
priority_queue<int> q;
int n = stations.size(), idx = 0;
int remain = startFuel, loc = 0, ans = 0;
while (loc < target) {
if (remain == 0) {
if (!q.empty() && ++ans >= 0) {
remain += q.top();
q.pop();
} else {
return -1;
}
}
loc += remain; remain = 0;
while (idx < n && stations[idx][0] <= loc) q.push(stations[idx++][1]);
}
return ans;
}
};

Python 代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Solution:
def minRefuelStops(self, target: int, startFuel: int, stations: List[List[int]]) -> int:
q = []
n, idx = len(stations), 0
remain, loc, ans = startFuel, 0, 0
while loc < target:
if remain == 0:
if q:
ans += 1
remain += -heappop(q)
else:
return -1
loc, remain = loc + remain, 0
while idx < n and stations[idx][0] <= loc:
heappush(q, -stations[idx][1])
idx += 1
return ans

  • 时间复杂度:$O(n\log{n})$
  • 空间复杂度:$O(n)$

最后

这是我们「刷穿 LeetCode」系列文章的第 No.871 篇,系列开始于 2021/01/01,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。

在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。

为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode

在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。