当前位置:网站首页>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;
}
};
边栏推荐
- 几行代码搞定RPC服务注册和发现
- Role and function of uboot
- Changwan group rushed to Hong Kong stocks: the annual revenue was 289million, and Liu Hui had 53.46% voting rights
- R及RStudio下载安装教程(超详细)
- 七张图,学会做有价值的经营分析
- 871. 最低加油次数
- The poor family once again gave birth to a noble son: Jiangxi poor county got the provincial number one, what did you do right?
- Kubernetes three open interfaces first sight
- 【Leetcode】13. Roman numeral to integer
- The computer comes with software to make the background color of the picture transparent (matting white background)
猜你喜欢

Easy language ABCD sort

【Leetcode】14. 最長公共前綴

Configure MySQL under Linux to authorize a user to access remotely, which is not restricted by IP

pwm呼吸燈

The poor family once again gave birth to a noble son: Jiangxi poor county got the provincial number one, what did you do right?

如何与博格华纳BorgWarner通过EDI传输业务数据?

Use the API port of the bridge of knowledge and action to provide resources for partners to access

亚马逊云科技 Community Builder 申请窗口开启

Changwan group rushed to Hong Kong stocks: the annual revenue was 289million, and Liu Hui had 53.46% voting rights

七张图,学会做有价值的经营分析
随机推荐
Cell: Tsinghua Chenggong group revealed an odor of skin flora. Volatiles promote flavivirus to infect the host and attract mosquitoes
【Leetcode】13. Roman numeral to integer
福元医药上交所上市:市值105亿 胡柏藩身价超40亿
In MySQL and Oracle, the boundary and range of between and precautions when querying the date
超卓航科上市:募资9亿市值超60亿 成襄阳首家科创板企业
LeetCode 3. Longest substring without duplicate characters
剑指 Offer 22. 链表中倒数第k个节点
剑指 Offer 21. 调整数组顺序使奇数位于偶数前面
基于Impala的高性能数仓实践之执行引擎模块
LeetCode 2. Add two numbers
[essay solicitation activity] Dear developer, RT thread community calls you to contribute
LSF basic command
AP and F107 data sources and processing
Green bamboo biological sprint Hong Kong stocks: loss of more than 500million during the year, tiger medicine and Beijing Yizhuang are shareholders
Role and function of uboot
Exploration and practice of integration of streaming and wholesale in jd.com
TCP congestion control details | 2 background
PWM breathing lamp
Interpretation of key parameters in MOSFET device manual
Privacy computing technology innovation and industry practice seminar: Learning