当前位置:网站首页>[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;
}
}
边栏推荐
- The Block:USDD增长势头强劲
- 上市公司改名,科学还是玄学?
- 【Hot100】31. Next spread
- 长城证券安全不 证券开户
- Face_recognition人脸识别之考勤统计
- What if Kaili can't input Chinese???
- 【HCIA持续更新】网络管理与运维
- Vb无法访问数据库stocks
- Blood spitting finishing nanny level series tutorial - play Fiddler bag grabbing tutorial (2) - first meet fiddler, let you have a rational understanding
- 超标量处理器设计 姚永斌 第6章 指令解码 摘录
猜你喜欢
随机推荐
Analysis of I2C adapter driver of s5pv210 chip (i2c-s3c2410. C)
超标量处理器设计 姚永斌 第5章 指令集体系 摘录
Detectron2 installation method
缓存穿透、缓存击穿、缓存雪崩分别是什么
wuzhicms代码审计
大规模服务异常日志检索
什么是低代码开发?
【系统分析师之路】第七章 复盘系统设计(结构化开发方法)
智捷云——元宇宙综合解决方案服务商
长城证券开户安全吗 证券账户怎么开通
一文掌握数仓中auto analyze的使用
【HCIA持续更新】WLAN工作流程概述
The Block:USDD增长势头强劲
整理混乱的头文件,我用include what you use
解读数据安全治理能力评估框架2.0,第四批DSG评估征集中
【Hot100】31. Next spread
KS007基于JSP实现人个人博客系统
完美融入 Win11 风格,微软全新 OneDrive 客户端抢先看
Superscalar processor design yaoyongbin Chapter 7 register rename excerpt
CANN算子:利用迭代器高效实现Tensor数据切割分块处理