当前位置:网站首页>[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;
}
}
边栏推荐
猜你喜欢
斑马识别成狗,AI犯错的原因被斯坦福找到了丨开源
Offline and open source version of notation -- comprehensive evaluation of note taking software anytype
Vscode modification indentation failed, indent four spaces as soon as it is saved
Vb无法访问数据库stocks
曾经的“彩电大王”,退市前卖猪肉
CocosCreator事件派发使用
Ble HCI flow control mechanism
上市公司改名,科学还是玄学?
Zebras are recognized as dogs, and the reason for AI's mistakes is found by Stanford
智捷云——元宇宙综合解决方案服务商
随机推荐
[template] [Luogu p4630] duathlon Triathlon (round square tree)
Set the transparent hidden taskbar and full screen display of the form
Internet addiction changes brain structure: language function is affected, making people unable to speak neatly
7 RSA Cryptosystem
12 - explore the underlying principles of IOS | runtime [isa details, class structure, method cache | t]
Web game engine
Vb无法访问数据库stocks
Interpretation of data security governance capability evaluation framework 2.0, the fourth batch of DSG evaluation collection
Cocoscreator event dispatch use
设置窗体透明 隐藏任务栏 与全屏显示
Display opencv drawn pictures on MFC picture control control
What is low code development?
CocosCreator事件派发使用
Developers, MySQL column finish, help you easily from installation to entry
比李嘉诚还有钱的币圈大佬,刚在沙特买了楼
就在今天丨汇丰4位专家齐聚,共讨银行核心系统改造、迁移、重构难题
What are cache penetration, cache breakdown, and cache avalanche
CANN算子:利用迭代器高效实现Tensor数据切割分块处理
【HCIA持续更新】WLAN概述与基本概念
关于nacos启动时防火墙开启8848的坑