当前位置:网站首页>【每日一题】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;
}
}
边栏推荐
- Ks007 realizes personal blog system based on JSP
- Ble HCI flow control mechanism
- 内核中时间相关的知识介绍
- detectron2安装方法
- 简单易用的地图可视化
- [HCIA continuous update] WLAN overview and basic concepts
- NFT流动性市场安全问题频发—NFT交易平台Quixotic被黑事件分析
- 将Opencv绘制图片显示在MFC Picture Control控件上
- Summary of tx.origin security issues
- Interpretation of data security governance capability evaluation framework 2.0, the fourth batch of DSG evaluation collection
猜你喜欢
Master the use of auto analyze in data warehouse
数学分析_笔记_第7章:多元函数的微分学
To sort out messy header files, I use include what you use
Superscalar processor design yaoyongbin Chapter 7 register rename excerpt
How to choose one plus 10 pro and iPhone 13?
What is low code development?
Solution of dealer collaboration system in building materials industry: empowering enterprises to build core competitiveness
Perfectly integrated into win11 style, Microsoft's new onedrive client is the first to see
Zhijieyun - meta universe comprehensive solution service provider
【HCIA持续更新】网络管理与运维
随机推荐
Is it safe for Bank of China Securities to open an account online?
新享科技发布小程序UniPro小优 满足客户移动办公场景
[HCIA continuous update] overview of WLAN workflow
防火墙基础透明模式部署和双机热备
TP configuring multiple databases
regular expression
Is it safe for Great Wall Securities to open an account? How to open a securities account
VB cannot access database stocks
码农版隐秘的角落:作为开发者最讨厌的5件
ARTS_20220628
利用win10计划任务程序定时自动运行jar包
[Huawei HCIA continuous update] SDN and FVC
就在今天丨汇丰4位专家齐聚,共讨银行核心系统改造、迁移、重构难题
Implementation of super large-scale warehouse clusters in large commercial banks
tx.origin安全问题总结
What if Kaili can't input Chinese???
大规模服务异常日志检索
通过事件绑定实现动画效果
Perfectly integrated into win11 style, Microsoft's new onedrive client is the first to see
Cann operator: using iterators to efficiently realize tensor data cutting and blocking processing