当前位置:网站首页>【每日一题】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;
}
}
边栏推荐
- Datakit -- the real unified observability agent
- Solution of dealer collaboration system in building materials industry: empowering enterprises to build core competitiveness
- NFT liquidity market security issues occur frequently - Analysis of the black incident of NFT trading platform quixotic
- 超标量处理器设计 姚永斌 第5章 指令集体系 摘录
- CocosCreator事件派发使用
- Is it safe for CITIC Securities to open an account online? Is the account opening fee charged
- 长城证券安全不 证券开户
- The Block:USDD增长势头强劲
- Talk about seven ways to realize asynchronous programming
- 【HCIA持续更新】广域网技术
猜你喜欢
第十八届IET交直流輸電國際會議(ACDC2022)於線上成功舉辦
防火墙基础透明模式部署和双机热备
数学分析_笔记_第7章:多元函数的微分学
使用3DMAX制作一枚手雷
超标量处理器设计 姚永斌 第6章 指令解码 摘录
CANN算子:利用迭代器高效实现Tensor数据切割分块处理
【Hot100】32. 最长有效括号
The company needs to be monitored. How do ZABBIX and Prometheus choose? That's the right choice!
The 18th IET AC / DC transmission International Conference (acdc2022) was successfully held online
Chow Tai Fook fulfills the "centenary commitment" and sincerely serves to promote green environmental protection
随机推荐
[test development] software testing - Basics
Implementation of super large-scale warehouse clusters in large commercial banks
Blood spitting finishing nanny level series tutorial - play Fiddler bag grabbing tutorial (2) - first meet fiddler, let you have a rational understanding
内核中时间相关的知识介绍
Vb无法访问数据库stocks
tx.origin安全问题总结
MVC mode and three-tier architecture
NFT liquidity market security issues occur frequently - Analysis of the black incident of NFT trading platform quixotic
[HCIA continuous update] WLAN overview and basic concepts
明星开店,退,退,退
Easy to use map visualization
R语言plotly可视化:plotly可视化互相重叠的直方图(historgram)、并在直方图的顶部边缘使用geom_rug函数添加边缘轴须图Marginal rug plots
Win32 API access route encrypted web pages
Firewall basic transparent mode deployment and dual machine hot standby
Vscode modification indentation failed, indent four spaces as soon as it is saved
Face_recognition人脸识别之考勤统计
整理混乱的头文件,我用include what you use
Zebras are recognized as dogs, and the reason for AI's mistakes is found by Stanford
Ble HCI flow control mechanism
Image retrieval