当前位置:网站首页>【每日一题】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;
}
}
边栏推荐
- It's too convenient. You can complete the code release and approval by nailing it!
- Load test practice of pingcode performance test
- Vb无法访问数据库stocks
- 如何进行MDM的产品测试
- 一文掌握数仓中auto analyze的使用
- How to choose one plus 10 pro and iPhone 13?
- 通过事件绑定实现动画效果
- Dynamic programming stock problem comparison
- 高中物理:力、物体和平衡
- detectron2安装方法
猜你喜欢

要上市的威马,依然给不了百度信心

detectron2安装方法
![[test development] software testing - Basics](/img/43/514016f270574fe711e0e15b581022.png)
[test development] software testing - Basics

超标量处理器设计 姚永斌 第5章 指令集体系 摘录

Mathematical analysis_ Notes_ Chapter 7: differential calculus of multivariate functions

Perfectly integrated into win11 style, Microsoft's new onedrive client is the first to see

一文掌握数仓中auto analyze的使用

什么是低代码开发?

整理混乱的头文件,我用include what you use

解决el-input输入框.number数字输入问题,去掉type=“number“后面箭头问题也可以用这种方法代替
随机推荐
Superscalar processor design yaoyongbin Chapter 5 instruction set excerpt
Internet addiction changes brain structure: language function is affected, making people unable to speak neatly
Vscode modification indentation failed, indent four spaces as soon as it is saved
无心剑中译伊丽莎白·毕肖普《一门技艺》
金额计算用 BigDecimal 就万无一失了?看看这五个坑吧~~
Superscalar processor design yaoyongbin Chapter 7 register rename excerpt
Win32 API access route encrypted web pages
Is it safe for Bank of China Securities to open an account online?
[system analyst's road] Chapter 7 double disk system design (structured development method)
Using win10 scheduling task program to automatically run jar package at fixed time
雨量预警广播自动化数据平台BWII 型广播预警监测仪
Kunming Third Ring Road Closure project will pass through these places. Is there one near your home?
KS007基于JSP实现人个人博客系统
什么是低代码开发?
聊聊异步编程的 7 种实现方式
就在今天丨汇丰4位专家齐聚,共讨银行核心系统改造、迁移、重构难题
【系统分析师之路】第七章 复盘系统设计(结构化开发方法)
7 RSA密码体制
detectron2安装方法
R language plot visualization: plot visualizes overlapping histograms and uses geom at the top edge of the histogram_ The rug function adds marginal rug plots