当前位置:网站首页>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;
}
};
边栏推荐
- 亚马逊云科技 Community Builder 申请窗口开启
- QStyle实现自绘界面项目实战(二)
- PhD Debate-11 预告 | 回顾与展望神经网络的后门攻击与防御
- 对接保时捷及3PL EDI案例
- Kubernetes three open interfaces first sight
- Privacy computing technology innovation and industry practice seminar: Learning
- 【Leetcode】13. Roman numeral to integer
- 人生的开始
- 【Leetcode】14. 最長公共前綴
- What is the difference between JSP and servlet?
猜你喜欢

DigiCert SSL证书支持中文域名申请吗?

相信自己,这次一把搞定JVM面试

Exploration of mobile application performance tools

Tech talk activity preview | building intelligent visual products based on Amazon kVs

Cell:清华程功组揭示皮肤菌群的一种气味挥发物促进黄病毒感染宿主吸引蚊虫...

871. 最低加油次数

Privacy computing technology innovation and industry practice seminar: Learning

福元医药上交所上市:市值105亿 胡柏藩身价超40亿

Connect Porsche and 3PL EDI cases

Day 18 of leetcode dynamic planning introduction
随机推荐
Seven charts, learn to do valuable business analysis
Talk about an experience of job hopping and being rejected
Exploration of mobile application performance tools
亚马逊云科技 Community Builder 申请窗口开启
VMware install win10 image
Learning Weekly - total issue 60 - 25th week of 2022
john爆破出現Using default input encoding: UTF-8 Loaded 1 password hash (bcrypt [Blowfish 32/64 X3])
Interpretation of key parameters in MOSFET device manual
一年顶十年
PWM controlled steering gear
PhD Debate-11 预告 | 回顾与展望神经网络的后门攻击与防御
Dgraph: large scale dynamic graph dataset
A few lines of code to complete RPC service registration and discovery
System Verilog实现优先级仲裁器
Weili holdings listed on the Hong Kong Stock Exchange: with a market value of HK $500million, it contributed an IPO to Hubei
超卓航科上市:募资9亿市值超60亿 成襄阳首家科创板企业
Just a coincidence? The mysterious technology of apple ios16 is even consistent with the products of Chinese enterprises five years ago!
Role and function of uboot
P6774 [NOI2020] 时代的眼泪(分块)
C语言自定义函数的方法