当前位置:网站首页>871. Minimum refueling times
871. Minimum refueling times
2022-07-02 17:12:00 【anieoo】
Original link :871. Minimum refueling times
solution:
This topic examines the priority queue + greedy . The meaning of the title is equivalent to carrying all the gasoline on your body during driving , If the car cannot reach the next gas station, add the barrel with the most gasoline , Finally, count the number of refueling .
class Solution {
public:
int minRefuelStops(int target, int startFuel, vector<vector<int>>& stations) {
if(stations.size() == 0) { // There is no gas station , Whether it can arrive depends only on the initial oil volume
if(startFuel >= target) return 0;
else return -1;
}
priority_queue<int,vector<int>,less<int>> heap; // Define a large root heap
int miles = startFuel; // The distance you can reach
int res = 0;// Refueling times
for(int i = 0;i < stations.size();i++) {
if(miles >= target) return res; // You can reach and jump out of the loop
while(miles < stations[i][0]) { // When the mileage is not enough to reach the next gas station
if(heap.empty()) return -1; // There is no gasoline to add , Can't arrive
auto oil = heap.top();
heap.pop();
miles += oil;
res++;
}
heap.push(stations[i][1]);
}
// I have brought all the gasoline , But we haven't reached the finish line for special judgment
while(miles < target) {
if(heap.empty()) return -1;
auto oil = heap.top();
heap.pop();
miles += oil;
res++;
}
return res;
}
};
边栏推荐
- 绿竹生物冲刺港股:年期内亏损超5亿 泰格医药与北京亦庄是股东
- 串口控制舵机转动
- 【征文活动】亲爱的开发者,RT-Thread社区喊你投稿啦
- IP address translation address segment
- John blasting appears using default input encoding: UTF-8 loaded 1 password hash (bcrypt [blowfish 32/64 x3])
- C语言自定义函数的方法
- System Verilog implements priority arbiter
- pwm呼吸燈
- 剑指 Offer 21. 调整数组顺序使奇数位于偶数前面
- JS delete substring in string
猜你喜欢
![[cloud native] briefly talk about the understanding of flume, a massive data collection component](/img/2d/8c4769e97fb84e98eafb7069551341.png)
[cloud native] briefly talk about the understanding of flume, a massive data collection component

QWebEngineView崩溃及替代方案

易语言abcd排序

剑指 Offer 22. 链表中倒数第k个节点
![[error record] error -32000 received from application: there are no running service protocol](/img/6c/66099650de46cac88b805e6cfb90b9.jpg)
[error record] error -32000 received from application: there are no running service protocol
![[leetcode] 14. Préfixe public le plus long](/img/70/e5be1a7c2e10776a040bfc8d7711a0.png)
[leetcode] 14. Préfixe public le plus long

QStyle实现自绘界面项目实战(二)

Seven charts, learn to do valuable business analysis

AP and F107 data sources and processing

Hard core! One configuration center for 8 classes!
随机推荐
john爆破出現Using default input encoding: UTF-8 Loaded 1 password hash (bcrypt [Blowfish 32/64 X3])
Digital IC hand tearing code -- voting device
Error when uploading code to remote warehouse: remote origin already exists
电脑自带软件使图片底色变为透明(抠图白底)
默认浏览器设置不了怎么办?
The impact of telecommuting on all aspects of our experience | community essay solicitation
JS delete substring in string
[cloud native] briefly talk about the understanding of flume, a massive data collection component
学习周刊-总第60期-2022年第25周
ThreadLocal
【Leetcode】13. 罗马数字转整数
剑指 Offer 26. 树的子结构
Penetration tool - intranet permission maintenance -cobalt strike
In MySQL and Oracle, the boundary and range of between and precautions when querying the date
LeetCode 4. Find the median (hard) of two positive arrays
2、 Expansion of mock platform
The macrogenome microbiome knowledge you want is all here (2022.7)
Deep learning image data automatic annotation [easy to understand]
如何与博格华纳BorgWarner通过EDI传输业务数据?
Just a coincidence? The mysterious technology of apple ios16 is even consistent with the products of Chinese enterprises five years ago!