当前位置:网站首页>[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;
}
}
边栏推荐
- 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
- With an annual income of more than 8 million, he has five full-time jobs. He still has time to play games
- The company needs to be monitored. How do ZABBIX and Prometheus choose? That's the right choice!
- Oppo Xiaobu launched Obert, a large pre training model, and promoted to the top of kgclue
- Is BigDecimal safe to calculate the amount? Look at these five pits~~
- 股价大跌、市值缩水,奈雪推出虚拟股票,深陷擦边球争议
- 俄罗斯 Arenadata 发布基于PostgreSQL的产品
- VSCode修改缩进不成功,一保存就缩进四个空格
- Offline and open source version of notation -- comprehensive evaluation of note taking software anytype
- 整理混乱的头文件,我用include what you use
猜你喜欢

Electronic pet dog - what is the internal structure?

超标量处理器设计 姚永斌 第6章 指令解码 摘录

Datakit -- the real unified observability agent

What if Kaili can't input Chinese???

补能的争议路线:快充会走向大一统吗?

【HCIA持续更新】网络管理与运维

雨量预警广播自动化数据平台BWII 型广播预警监测仪

Zebras are recognized as dogs, and the reason for AI's mistakes is found by Stanford

智捷云——元宇宙综合解决方案服务商

Mathematical analysis_ Notes_ Chapter 7: differential calculus of multivariate functions
随机推荐
78岁华科教授冲击IPO,丰年资本有望斩获数十倍回报
股价大跌、市值缩水,奈雪推出虚拟股票,深陷擦边球争议
【每日一题】556. 下一个更大元素 III
Great Wall Securities security does not open a securities account
To sort out messy header files, I use include what you use
【Hot100】32. Longest valid bracket
码农版隐秘的角落:作为开发者最讨厌的5件
智捷云——元宇宙综合解决方案服务商
中断的顶半部和底半部介绍以及实现方式(tasklet 和 工作队列)
Master the use of auto analyze in data warehouse
庆贺!科蓝SUNDB与中创软件完成七大产品的兼容性适配
Developers, MySQL column finish, help you easily from installation to entry
RecastNavigation 之 Recast
超大规模数仓集群在大型商业银行的落地实践
Blood spitting finishing nanny level series tutorial - play Fiddler bag grabbing tutorial (2) - first meet fiddler, let you have a rational understanding
解决el-input输入框.number数字输入问题,去掉type=“number“后面箭头问题也可以用这种方法代替
长城证券开户安全吗 证券账户怎么开通
【Hot100】31. 下一个排列
Set the transparent hidden taskbar and full screen display of the form
KS007基于JSP实现人个人博客系统