当前位置:网站首页>[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
- Zebras are recognized as dogs, and the reason for AI's mistakes is found by Stanford
- 解决el-input输入框.number数字输入问题,去掉type=“number“后面箭头问题也可以用这种方法代替
- [system analyst's road] Chapter 7 double disk system design (structured development method)
- 超标量处理器设计 姚永斌 第6章 指令解码 摘录
- 你应该懂些CI/CD
- Internet addiction changes brain structure: language function is affected, making people unable to speak neatly
- DB-Engines 2022年7月数据库排行榜:Microsoft SQL Server 大涨,Oracle 大跌
- Superscalar processor design yaoyongbin Chapter 5 instruction set excerpt
- 解读数据安全治理能力评估框架2.0,第四批DSG评估征集中
猜你喜欢

明星开店,退,退,退

CANN算子:利用迭代器高效实现Tensor数据切割分块处理

【测试开发】软件测试——基础篇

电子宠物小狗-内部结构是什么?

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

Detectron2 installation method

Firewall basic transparent mode deployment and dual machine hot standby

DB-Engines 2022年7月数据库排行榜:Microsoft SQL Server 大涨,Oracle 大跌

上市公司改名,科学还是玄学?

【Hot100】32. Longest valid bracket
随机推荐
上市公司改名,科学还是玄学?
Master the use of auto analyze in data warehouse
regular expression
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
“在越南,钱就像躺在街上”
[HCIA continuous update] overview of WLAN workflow
【Hot100】31. 下一个排列
估值900亿,超级芯片IPO来了
通过事件绑定实现动画效果
整理混乱的头文件,我用include what you use
【每日一题】871. 最低加油次数
curl 命令妙用
The company needs to be monitored. How do ZABBIX and Prometheus choose? That's the right choice!
Blood spitting finishing nanny level series tutorial - play Fiddler bag grabbing tutorial (2) - first meet fiddler, let you have a rational understanding
Pytoch deep learning environment construction
How to test MDM products
kaili不能输入中文怎么办???
Internet addiction changes brain structure: language function is affected, making people unable to speak neatly
KS007基于JSP实现人个人博客系统
[proteus simulation] printf debugging output example based on VSM serial port