当前位置:网站首页>[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;
}
}
边栏推荐
- [test development] software testing - Basics
- 【HCIA持续更新】WLAN概述与基本概念
- Initial experience of domestic database tidb: simple and easy to use, quick to start
- You should know something about ci/cd
- 估值900亿,超级芯片IPO来了
- 内核中时间相关的知识介绍
- 7 RSA Cryptosystem
- 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"
- 整理混乱的头文件,我用include what you use
- 【Hot100】31. 下一个排列
猜你喜欢
《吐血整理》保姆级系列教程-玩转Fiddler抓包教程(2)-初识Fiddler让你理性认识一下

表情包坑惨职场人

Make a grenade with 3DMAX

To sort out messy header files, I use include what you use

Hidden corners of coder Edition: five things that developers hate most

CocosCreator事件派发使用

雨量预警广播自动化数据平台BWII 型广播预警监测仪
Blood spitting finishing nanny level series tutorial - play Fiddler bag grabbing tutorial (2) - first meet fiddler, let you have a rational understanding

With an annual income of more than 8 million, he has five full-time jobs. He still has time to play games

【Hot100】32. Longest valid bracket
随机推荐
《吐血整理》保姆级系列教程-玩转Fiddler抓包教程(2)-初识Fiddler让你理性认识一下
Web game engine
【Hot100】32. Longest valid bracket
Rainfall warning broadcast automatic data platform bwii broadcast warning monitor
CocosCreator事件派发使用
Using win10 scheduling task program to automatically run jar package at fixed time
【Hot100】31. 下一个排列
Initial experience of domestic database tidb: simple and easy to use, quick to start
The Block:USDD增长势头强劲
Set the transparent hidden taskbar and full screen display of the form
缓存穿透、缓存击穿、缓存雪崩分别是什么
【HCIA持续更新】WLAN工作流程概述
你应该懂些CI/CD
R language plot visualization: plot visualization of multiple variable violin plot in R with plot
Recast of recastnavigation
Dynamic programming stock problem comparison
You should know something about ci/cd
【每日一题】556. 下一个更大元素 III
【系统分析师之路】第七章 复盘系统设计(结构化开发方法)
R语言plotly可视化:plotly可视化互相重叠的直方图(historgram)、并在直方图的顶部边缘使用geom_rug函数添加边缘轴须图Marginal rug plots