当前位置:网站首页>【每日一题】871. 最低加油次数
【每日一题】871. 最低加油次数
2022-07-04 16:04:00 【王六六的IT日常】
871. 最低加油次数
困难题
参考:【宫水三叶】简单优先队列(堆)贪心题
题目思路:
- 将路上的一个个加油站 视为 一桶桶的油,每次经过的时候,就把油带上放后备箱;
- 当油不够的时候,取出后备箱所带的 最多的那桶油 加进油箱
- 这样以来,如若油箱和后备箱的油加起来都不够,那么就到不了了
class Solution {
public int minRefuelStops(int target, int startFuel, int[][] stations) {
// 使用优先队列,承装所经过加油站的油
PriorityQueue<Integer> q = new PriorityQueue<>((o1, o2) -> (o2 - o1));
int ans = 0, len = stations.length;
// 特判:
if (len < 1) return startFuel < target ? -1 : 0;
int fuel = startFuel;// 加进油箱的油(含使用过的)
// 经过可以到达的所有的加油站,背上里面的油
for (int i = 0; i < len; i ++) {
while (fuel < stations[i][0]) {
Integer add = q.poll();
if (add == null) return -1;
fuel += add;
ans ++;
}
q.offer(stations[i][1]);
}
// 已经经过所有的加油站仍未到达,则用车油箱和后备箱里的所剩的fuel,以期到达
while (fuel < target) {
Integer add = q.poll();
if (add == null) return -1;
fuel += add;
ans ++;
}
return ans;
}
}
边栏推荐
- detectron2安装方法
- Detectron2 installation method
- Ks007 realizes personal blog system based on JSP
- 超大规模数仓集群在大型商业银行的落地实践
- 【HCIA持续更新】WLAN概述与基本概念
- [HCIA continuous update] overview of WLAN workflow
- 【系统分析师之路】第七章 复盘系统设计(结构化开发方法)
- 利用win10计划任务程序定时自动运行jar包
- Summary of tx.origin security issues
- Master the use of auto analyze in data warehouse
猜你喜欢

NFT流动性市场安全问题频发—NFT交易平台Quixotic被黑事件分析

【Unity UGUI】ScrollRect 动态缩放格子大小,自动定位到中间的格子

Easy to use map visualization

离线、开源版的 Notion—— 笔记软件Anytype 综合评测

曾经的“彩电大王”,退市前卖猪肉

【HCIA持续更新】网络管理与运维

The 18th IET AC / DC transmission International Conference (acdc2022) was successfully held online

Offline and open source version of notation -- comprehensive evaluation of note taking software anytype

居家打工年入800多万,一共五份全职工作,他还有时间打游戏

Zebras are recognized as dogs, and the reason for AI's mistakes is found by Stanford
随机推荐
Solve the El input input box For number number input problem, this method can also be used to replace the problem of removing the arrow after type= "number"
VSCode修改缩进不成功,一保存就缩进四个空格
New technology releases a small program UNIPRO to meet customers' mobile office scenarios
Heartless sword Chinese translation of Elizabeth Bishop's a skill
CANN算子:利用迭代器高效实现Tensor数据切割分块处理
CocosCreator事件派發使用
The 18th IET AC / DC transmission International Conference (acdc2022) was successfully held online
Initial experience of domestic database tidb: simple and easy to use, quick to start
What if Kaili can't input Chinese???
The company needs to be monitored. How do ZABBIX and Prometheus choose? That's the right choice!
Two methods of MD5 encryption
[HCIA continuous update] overview of WLAN workflow
Dynamic programming stock problem comparison
MVC mode and three-tier architecture
一文掌握数仓中auto analyze的使用
To sort out messy header files, I use include what you use
开发者,MySQL专栏完更,助你轻松从安装到入门进阶
Cann operator: using iterators to efficiently realize tensor data cutting and blocking processing
Vscode modification indentation failed, indent four spaces as soon as it is saved
Superscalar processor design yaoyongbin Chapter 7 register rename excerpt