当前位置:网站首页>[daily question] 871 Minimum refueling times
[daily question] 871 Minimum refueling times
2022-07-04 17:59:00 【Wang Liuliu's it daily】
871. Minimum refueling times
Difficult questions
Reference resources :【 Gongshui Sanye 】 Simple priority queue ( Pile up ) Greedy question
Topic ideas :
- The gas stations on the road As Barrels of oil , Every time I pass , Just take the oil and put it in the trunk ;
- When there is not enough oil , Take out what you have in the trunk The largest barrel of oil Fill the oil tank
- Since then , If the oil in the tank and the trunk are not enough , Then it won't arrive
class Solution {
public int minRefuelStops(int target, int startFuel, int[][] stations) {
// Use priority queues , Load the oil passing through the gas station
PriorityQueue<Integer> q = new PriorityQueue<>((o1, o2) -> (o2 - o1));
int ans = 0, len = stations.length;
// Special judgement :
if (len < 1) return startFuel < target ? -1 : 0;
int fuel = startFuel;// The oil added into the tank ( Including used )
// Pass all the gas stations you can reach , The oil on your back
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]);
}
// We have passed all the gas stations and still haven't arrived , What's left in the car's fuel tank and trunk fuel, Expect to arrive
while (fuel < target) {
Integer add = q.poll();
if (add == null) return -1;
fuel += add;
ans ++;
}
return ans;
}
}
边栏推荐
- Mathematical analysis_ Notes_ Chapter 7: differential calculus of multivariate functions
- 上网成瘾改变大脑结构:语言功能受影响,让人话都说不利索
- MVC mode and three-tier architecture
- 简单易用的地图可视化
- 什么是低代码开发?
- Great Wall Securities security does not open a securities account
- 数学分析_笔记_第7章:多元函数的微分学
- Interpretation of data security governance capability evaluation framework 2.0, the fourth batch of DSG evaluation collection
- ARTS_ twenty million two hundred and twenty thousand six hundred and twenty-eight
- regular expression
猜你喜欢
CANN算子:利用迭代器高效实现Tensor数据切割分块处理
CocosCreator事件派發使用
超标量处理器设计 姚永斌 第7章 寄存器重命名 摘录
Mathematical analysis_ Notes_ Chapter 7: differential calculus of multivariate functions
90后开始攒钱植发,又一个IPO来了
就在今天丨汇丰4位专家齐聚,共讨银行核心系统改造、迁移、重构难题
要上市的威马,依然给不了百度信心
RecastNavigation 之 Recast
[unity ugui] scrollrect dynamically scales the grid size and automatically locates the middle grid
我写了一份初学者的学习实践教程!
随机推荐
High school physics: force, object and balance
Offline and open source version of notation -- comprehensive evaluation of note taking software anytype
Perfectly integrated into win11 style, Microsoft's new onedrive client is the first to see
解决el-input输入框.number数字输入问题,去掉type=“number“后面箭头问题也可以用这种方法代替
Win32 API 访问路由的加密网页
Cocoscreator event dispatch use
什么是低代码开发?
Make a grenade with 3DMAX
高中物理:力、物体和平衡
表情包坑惨职场人
【Hot100】31. Next spread
长城证券安全不 证券开户
完美融入 Win11 风格,微软全新 OneDrive 客户端抢先看
庆贺!科蓝SUNDB与中创软件完成七大产品的兼容性适配
Achieve animation effect through event binding
VB cannot access database stocks
Face_ Attendance statistics of recognition face recognition
动态规划股票问题对比
内核中时间相关的知识介绍
[template] [Luogu p4630] duathlon Triathlon (round square tree)