当前位置:网站首页>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;
}
};
边栏推荐
- 【日常训练】871. 最低加油次数
- AttributeError: ‘tuple‘ object has no attribute ‘layer‘问题解决
- Multiprocess programming (I): basic concepts
- 微信小程序获取某个元素的信息(高、宽等),并将px转换为rpx。
- 【雅思阅读】王希伟阅读P2(阅读填空)
- Is there a free text to speech tool to help recommend?
- 图解网络:什么是虚拟路由器冗余协议 VRRP?
- Markdown tutorial
- Cordova plugin device obtains the device information plug-in, which causes Huawei to fail the audit
- Helm basic learning
猜你喜欢

Nacos+openfeign error reporting solution

Sentry developer contribution Guide - configure pycharm

FAQ | FAQ for building applications for large screen devices

One of the reasons why setinterval timer does not take effect in ie: the callback is the arrow function

Multiprocess programming (I): basic concepts
![[shutter] image component (load network pictures | load static pictures | load local pictures | path | provider plug-in)](/img/7e/4f9d96edd04e9ffb26434baf34aa43.jpg)
[shutter] image component (load network pictures | load static pictures | load local pictures | path | provider plug-in)

Vulkan-实践第一弹

logback配置文件

图解网络:什么是虚拟路由器冗余协议 VRRP?

How SQLSEVER removes data with duplicate IDS
随机推荐
The most painful programming problem in 2021, adventure of code 2021 Day24
Unity learns from spaceshooter to record the difference between fixedupdate and update in unity for the second time
Hundreds of continuous innovation to create free low code office tools
【AutoSAR 八 OS】
可下载《2022年中国数字化办公市场研究报告》详解1768亿元市场
How to find out the currently running version of Solr- How do I find out version of currently running Solr?
【AutoSAR 三 RTE概述】
Introduction of UART, RS232, RS485, I2C and SPI
多进程编程(二):管道
【AutoSAR 十 IO架构】
JSON conversion tool class
antv x6节点拖拽到画布上后的回调事件(踩大坑记录)
Callback event after the antv X6 node is dragged onto the canvas (stepping on a big hole record)
How SQLSEVER removes data with duplicate IDS
AEM: Nanlin fan Ben et al. - plant rhizosphere growth promoting bacteria control soybean blight
Automated defect analysis in electron microscopic images-论文阅读笔记
University of Toronto: Anthony coach | the conditions of deep reinforcement learning can induce dynamic risk measurement
【案例分享】让新时代教育发展与“数”俱进
[shutter] image component (the placeholder | transparent_image transparent image plug-in is loaded into the memory)
Detailed explanation of pod life cycle