当前位置:网站首页>871. 最低加油次数 : 简单优先队列(堆)贪心题
871. 最低加油次数 : 简单优先队列(堆)贪心题
2022-07-02 11:32:00 【宫水三叶的刷题日记】
题目描述
这是 LeetCode 上的 871. 最低加油次数 ,难度为 困难。
Tag : 「贪心」、「优先队列(堆)」、「模拟」
汽车从起点出发驶向目的地,该目的地位于出发位置东面 target
英里处。
沿途有加油站,每个 station[i]
代表一个加油站,它位于出发位置东面 station[i][0]
英里处,并且有 station[i][1]
升汽油。
假设汽车油箱的容量是无限的,其中最初有 startFuel
升燃料。它每行驶 英里就会用掉 升汽油。
当汽车到达加油站时,它可能停下来加油,将所有汽油从加油站转移到汽车中。
为了到达目的地,汽车所必要的最低加油次数是多少?如果无法到达目的地,则返回 。
注意:如果汽车到达加油站时剩余燃料为 ,它仍然可以在那里加油。如果汽车到达目的地时剩余燃料为 ,仍然认为它已经到达目的地。
示例 1:
输入:target = 1, startFuel = 1, stations = []
输出:0
解释:我们可以在不加油的情况下到达目的地。
示例 2:
输入:target = 100, startFuel = 1, stations = [[10,100]]
输出:-1
解释:我们无法抵达目的地,甚至无法到达第一个加油站。
示例 3:
输入: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 。
提示:
贪心 + 优先队列(堆)
为了方便,我们记 target
为 t
,记 startFuel
为 start
,记 stations
为 ss
。
我们可以模拟行进过程,使用 n
代表加油站总个数,idx
代表经过的加油站下标,使用 remain
代表当前有多少油(起始有 remain = start
),loc
代表走了多远,ans
代表答案(至少需要的加油次数)。
只要 loc < t
,代表还没到达(经过)目标位置,我们可以继续模拟行进过程,每次将 remain
累加到 loc
中,含义为使用完剩余的油量,可以去到的最远距离,同时将所在位置 ss[idx][0] <= loc
的加油站数量加入优先队列(大根堆,根据油量排倒序)中,再次检查是否满足 loc < t
(下次循环),此时由于清空了剩余油量 remain
,我们尝试从优先队列(大根堆)中取出过往油量最大的加油站并进行加油(同时对加油次数 ans
进行加一操作),使用新的剩余油量 remain
重复上述过程,直到满足 loc >= t
或无油可加。
容易证明该做法的正确性:同样是消耗一次加油次数,始终选择油量最大的加油站进行加油,可以确保不存在更优的结果。
代码:
class Solution {
public int minRefuelStops(int t, int start, int[][] ss) {
PriorityQueue<Integer> q = new PriorityQueue<>((a,b)->b-a);
int n = ss.length, idx = 0;
int remain = start, loc = 0, ans = 0;
while (loc < t) {
if (remain == 0) {
if (!q.isEmpty() && ++ans >= 0) remain += q.poll();
else return -1;
}
loc += remain; remain = 0;
while (idx < n && ss[idx][0] <= loc) q.add(ss[idx++][1]);
}
return ans;
}
}
时间复杂度: 空间复杂度:
最后
这是我们「刷穿 LeetCode」系列文章的第 No.871
篇,系列开始于 2021/01/01,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。
在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。
为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode 。
在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。
边栏推荐
- It's no exaggeration to say that this is the most user-friendly basic tutorial of pytest I've ever seen
- 2. Const pointer
- jmeter脚本参数化
- Use of freemaker
- 【NOI模拟赛】伊莉斯elis(贪心,模拟)
- 检查密码
- C语言高级用法--函数指针:回调函数;转换表
- MathML to latex
- Contrôleur pour threejs cube Space Basic Controller + Inertial Control + Flight Control
- 【NOI模拟赛】刮痧(动态规划)
猜你喜欢
Available solution development oral arithmetic training machine / math treasure / children's oral arithmetic treasure / intelligent math treasure LCD LCD driver ic-vk1622 (lqfp64 package), original te
A white hole formed by antineutrons produced by particle accelerators
Pychart connects to the remote server
广州市应急管理局发布7月高温高湿化工安全提醒
Makefile 分隔文件名与后缀
<口算練習機 方案開發原理圖>口算練習機/口算寶/兒童數學寶/兒童計算器 LCD液晶顯示驅動IC-VK1621B,提供技術支持
跨服务器数据访问的创建链接服务器方法
Yyds dry goods inventory software encryption lock function
Chinese science and technology from the Winter Olympics (III): the awakening and evolution of digital people
What is erdma? Popular science cartoon illustration
随机推荐
Fabric.js 上划线、中划线(删除线)、下划线
字符串匹配问题
Advanced C language (realize simple address book)
Error: NPM warn config global ` --global`, `--local` are deprecated Use `--location=global` instead.
Factal: Unsafe repository is owned by someone else Solution
Bit by bit of OpenCV calling USB camera
Use of freemaker
Fabric. JS dynamically set font size
Advanced usage of C language -- function pointer: callback function; Conversion table
threejs的控制器 立方体空间 基本控制器+惯性控制+飞行控制
使用mathtype编辑公式,复制粘贴时设置成仅包含mathjax语法的公式
检查密码
求轮廓最大内接圆
【题解】Educational Codeforces Round 82
广州市应急管理局发布7月高温高湿化工安全提醒
ONNX+TensorRT:将预处理操作写入ONNX并完成TRT部署
Fatal: unsafe repository is owned by someone else
Tip: SQL Server blocked the state 'openrowset/opendatasource' of component 'ad hoc distributed queries'
tmall. product. schema. Get (product information acquisition schema acquisition), Taobao store upload commodity API interface, Taobao commodity publishing interface, Taobao commodity upload API interf
The use of TestNG, the testing framework (II): the use of TestNG XML