当前位置:网站首页>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;
}
};
边栏推荐
- Rust ownership (very important)
- AttributeError: ‘tuple‘ object has no attribute ‘layer‘问题解决
- 利亚德:Micro LED 产品消费端首先针对 100 英寸以上电视,现阶段进入更小尺寸还有难度
- lex && yacc && bison && flex 配置的问题
- MySQL 23 classic interview hanging interviewer
- Nacos+openfeign error reporting solution
- NC20806 区区区间间间
- 2022中国3D视觉企业(引导定位、分拣场景)厂商名单
- File operation io-part2
- Some introduction and precautions about XML
猜你喜欢
随机推荐
详解用OpenCV的轮廓检测函数findContours()得到的轮廓拓扑结构(hiararchy)矩阵的意义、以及怎样用轮廓拓扑结构矩阵绘制轮廓拓扑结构图
Centos7 one click compilation to build MySQL script
图解网络:什么是虚拟路由器冗余协议 VRRP?
An excellent orm in dotnet circle -- FreeSQL
[shutter] image component (load network pictures | load static pictures | load local pictures | path | provider plug-in)
mm中的GAN模型架构
2022上半年值得被看见的10条文案,每一句都能带给你力量!
Unity learns from spaceshooter to record the difference between fixedupdate and update in unity for the second time
【小程序项目开发-- 京东商城】uni-app之自定义搜索组件(中)-- 搜索建议
Why is the website slow to open?
Callback event after the antv X6 node is dragged onto the canvas (stepping on a big hole record)
The most painful programming problem in 2021, adventure of code 2021 Day24
为什么网站打开速度慢?
UART、RS232、RS485、I2C和SPI的介绍
Multiprocess programming (4): shared memory
[pulsar document] concepts and architecture
NC24840 [USACO 2009 Mar S]Look Up
node_ Modules cannot be deleted
One of the reasons why setinterval timer does not take effect in ie: the callback is the arrow function
lex && yacc && bison && flex 配置的問題








