当前位置:网站首页>leetcode-871:最低加油次数
leetcode-871:最低加油次数
2022-07-02 23:55:00 【菊头蝙蝠】
题目
汽车从起点出发驶向目的地,该目的地位于出发位置东面 target 英里处。
沿途有加油站,每个 station[i] 代表一个加油站,它位于出发位置东面 station[i][0] 英里处,并且有 station[i][1] 升汽油。
假设汽车油箱的容量是无限的,其中最初有 startFuel 升燃料。它每行驶 1 英里就会用掉 1 升汽油。
当汽车到达加油站时,它可能停下来加油,将所有汽油从加油站转移到汽车中。
为了到达目的地,汽车所必要的最低加油次数是多少?如果无法到达目的地,则返回 -1 。
注意:如果汽车到达加油站时剩余燃料为 0,它仍然可以在那里加油。如果汽车到达目的地时剩余燃料为 0,仍然认为它已经到达目的地。
解题
方法一:贪心
这个解法和leetcode-45:跳跃游戏 II是类似的,计算每次的最大覆盖距离。第几次可以覆盖终点,就说明要几次。
class Solution {
public:
int minRefuelStops(int target, int startFuel, vector<vector<int>>& stations) {
priority_queue<int> q;
int res=0;
int i=0;
while(startFuel<target){
while(i<stations.size()&&startFuel>=stations[i][0]){
q.push(stations[i++][1]);
}
if(q.empty()) return -1;
startFuel+=q.top();
q.pop();
res++;
}
return res;
}
};
边栏推荐
- lex && yacc && bison && flex 配置的問題
- Vulkan并非“灵药“
- 字符设备注册常用的两种方法和步骤
- 【AutoSAR 八 OS】
- NC24325 [USACO 2012 Mar S]Flowerpot
- Rust ownership (very important)
- Liad: the consumer end of micro LED products is first targeted at TVs above 100 inches. At this stage, it is still difficult to enter a smaller size
- Some introduction and precautions about XML
- About the practice topic of screen related to unity screen, unity moves around a certain point inside
- Vulkan is not a "panacea"“
猜你喜欢

Linux软件:如何安装Redis服务

为什么网站打开速度慢?

logback配置文件
![[shutter] image component (the placeholder | transparent_image transparent image plug-in is loaded into the memory)](/img/73/19e2e0fc5ea6f05e34584ba40a452d.jpg)
[shutter] image component (the placeholder | transparent_image transparent image plug-in is loaded into the memory)

pageoffice-之bug修改之旅

MySQL 23 classic interview hanging interviewer

利亚德:Micro LED 产品消费端首先针对 100 英寸以上电视,现阶段进入更小尺寸还有难度

百数不断创新,打造自由的低代码办公工具
![[MCU project training] eight way answering machine](/img/a3/6a50619cd16269bf485a4a273677aa.jpg)
[MCU project training] eight way answering machine

【小程序项目开发-- 京东商城】uni-app之自定义搜索组件(中)-- 搜索建议
随机推荐
机器学习:numpy版本线性回归预测波士顿房价
Set up nacos2 X cluster steps and problems encountered
Nacos+openfeign error reporting solution
Overlay of shutter (Pop-Up)
File operation io-part2
2022上半年值得被看见的10条文案,每一句都能带给你力量!
【案例分享】让新时代教育发展与“数”俱进
【AutoSAR 十三 NVM】
关于QByteArray存储十六进制 与十六进制互转
[jetcache] jetcache configuration description and annotation attribute description
多进程编程(二):管道
Nc20806 District interval
【AutoSAR 七 工具链简介】
helm 基础学习
How SQLSEVER removes data with duplicate IDS
Graduation summary
Two common methods and steps of character device registration
Multiprocess programming (4): shared memory
【雅思阅读】王希伟阅读P1(阅读判断题)
利亚德:Micro LED 产品消费端首先针对 100 英寸以上电视,现阶段进入更小尺寸还有难度