当前位置:网站首页>[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;
}
}
边栏推荐
猜你喜欢
Blood spitting finishing nanny level series tutorial - play Fiddler bag grabbing tutorial (2) - first meet fiddler, let you have a rational understanding

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

How to test MDM products

完美融入 Win11 风格,微软全新 OneDrive 客户端抢先看

明星开店,退,退,退
《吐血整理》保姆级系列教程-玩转Fiddler抓包教程(2)-初识Fiddler让你理性认识一下

什么是低代码开发?

我写了一份初学者的学习实践教程!

MVC mode and three-tier architecture

CANN算子:利用迭代器高效实现Tensor数据切割分块处理
随机推荐
Cann operator: using iterators to efficiently realize tensor data cutting and blocking processing
一文掌握数仓中auto analyze的使用
Ks007 realizes personal blog system based on JSP
解读数据安全治理能力评估框架2.0,第四批DSG评估征集中
R language plot visualization: plot visualization of multiple variable violin plot in R with plot
正则表达式
【Hot100】31. 下一个排列
Datakit -- the real unified observability agent
股价大跌、市值缩水,奈雪推出虚拟股票,深陷擦边球争议
公司要上监控,Zabbix 和 Prometheus 怎么选?这么选准没错!
The top half and bottom half of the interrupt are introduced and the implementation method (tasklet and work queue)
Cocoscreator event dispatch use
Superscalar processor design yaoyongbin Chapter 7 register rename excerpt
Clever use of curl command
What is low code development?
wuzhicms代码审计
曾经的“彩电大王”,退市前卖猪肉
Firewall basic transparent mode deployment and dual machine hot standby
Is BigDecimal safe to calculate the amount? Look at these five pits~~
[Huawei HCIA continuous update] SDN and FVC