当前位置:网站首页>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;
}
};
边栏推荐
- Win10 多种方式解决无法安装.Net3.5的问题
- Introduction and use of ftrace tool
- 【AutoSAR 二 AppL概述】
- kubernetes编写yml简单入门
- Markdown tutorial
- 可下载《2022年中国数字化办公市场研究报告》详解1768亿元市场
- One of the reasons why setinterval timer does not take effect in ie: the callback is the arrow function
- 【AutoSAR 四 BSW概述】
- Attributeerror: 'tuple' object has no attribute 'layer' problem solving
- Vulkan并非“灵药“
猜你喜欢
【AutoSAR 十一 通信相关机制】
【AutoSAR 六 描述文件】
Baidu AI Cloud takes the lead in building a comprehensive and standardized platform for smart cloud
1.12 - 指令
University of Toronto: Anthony coach | the conditions of deep reinforcement learning can induce dynamic risk measurement
Vulkan-性能及精细化
Multiprocess programming (II): Pipeline
Web2.0 giants have deployed VC, and tiger Dao VC may become a shortcut to Web3
Two common methods and steps of character device registration
Rust string slicing, structs, and enumeration classes
随机推荐
An excellent orm in dotnet circle -- FreeSQL
多进程编程(三):消息队列
Teach you JDBC hand in hand -- structure separation
There is an unknown problem in inserting data into the database
University of Oslo: Li Meng | deep reinforcement learning based on swing transformer
字符设备注册常用的两种方法和步骤
Lex & yacc & bison & flex configuration problems
【AutoSAR 五 方法论】
[IELTS reading] Wang Xiwei reading P1 (reading judgment question)
FAQ | FAQ for building applications for large screen devices
University of Toronto:Anthony Coache | 深度强化学习的条件可诱导动态风险度量
1.11 - 总线
[MCU project training] eight way answering machine
Vulkan-性能及精细化
Redis21 classic interview questions, extreme pull interviewer
LeedCode1480. Dynamic sum of one-dimensional array
2022中国3D视觉企业(引导定位、分拣场景)厂商名单
Introduction and use of ftrace tool
多进程编程(一):基本概念
Markdown tutorial