当前位置:网站首页>871. 最低加油次数
871. 最低加油次数
2022-07-02 14:21:00 【anieoo】
原题链接:871. 最低加油次数
solution:
本题考察优先队列+贪心。题目的意思是相当于在行驶的过程中把途径的汽油全部携带在身上,如果汽车无法抵达下一个加油站则把汽油最多的那个油桶加上,最后统计加油的次数。
class Solution {
public:
int minRefuelStops(int target, int startFuel, vector<vector<int>>& stations) {
if(stations.size() == 0) { //没有加油站,能否抵达只取决于初始油量
if(startFuel >= target) return 0;
else return -1;
}
priority_queue<int,vector<int>,less<int>> heap; //定义大根堆
int miles = startFuel; //可以抵达的距离
int res = 0;//加油次数
for(int i = 0;i < stations.size();i++) {
if(miles >= target) return res; //可以抵达跳出循环
while(miles < stations[i][0]) { //当公里数不够抵达下一个加油站的时候
if(heap.empty()) return -1; //没有汽油可加,不能抵达
auto oil = heap.top();
heap.pop();
miles += oil;
res++;
}
heap.push(stations[i][1]);
}
//已经携带了全部的汽油,但还没抵达终点进行特判
while(miles < target) {
if(heap.empty()) return -1;
auto oil = heap.top();
heap.pop();
miles += oil;
res++;
}
return res;
}
};
边栏推荐
- Deep learning image data automatic annotation [easy to understand]
- LeetCode 5. Longest Palindromic Substring
- R及RStudio下载安装教程(超详细)
- jsp 和 servlet 有什么区别?
- System Verilog实现优先级仲裁器
- Penetration tool - intranet permission maintenance -cobalt strike
- IP address translation address segment
- Atcoder beginer contest 169 (B, C, D unique decomposition, e mathematical analysis f (DP))
- Error when uploading code to remote warehouse: remote origin already exists
- Amazon cloud technology community builder application window opens
猜你喜欢

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

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

A week of short video platform 30W exposure, small magic push helps physical businesses turn losses into profits

基于Impala的高性能数仓实践之执行引擎模块

Rock PI Development Notes (II): start with rock PI 4B plus (based on Ruixing micro rk3399) board and make system operation

LeetCode 2. Add two numbers

OpenPose的使用

Yolov5 practice: teach object detection by hand

配置基于接口的ARP表项限制和端口安全(限制用户私自接入傻瓜交换机或非法主机接入)
![john爆破出現Using default input encoding: UTF-8 Loaded 1 password hash (bcrypt [Blowfish 32/64 X3])](/img/4c/ddf7f8085257d0eb8766dbec251345.png)
john爆破出現Using default input encoding: UTF-8 Loaded 1 password hash (bcrypt [Blowfish 32/64 X3])
随机推荐
[fluent] dart data type list set type (define set | initialize | generic usage | add elements after initialization | set generation function | set traversal)
Global and Chinese market of jacquard looms 2022-2028: Research Report on technology, participants, trends, market size and share
john爆破出现Using default input encoding: UTF-8 Loaded 1 password hash (bcrypt [Blowfish 32/64 X3])
深度学习图像数据自动标注[通俗易懂]
Youzan won the "top 50 Chinese enterprise cloud technology service providers" together with Tencent cloud and Alibaba cloud [easy to understand]
john爆破出現Using default input encoding: UTF-8 Loaded 1 password hash (bcrypt [Blowfish 32/64 X3])
Xiaopeng P7 had an accident on rainy days, and the airbag did not pop up. Official response: the impact strength did not meet the ejection requirements
配置基于接口的ARP表项限制和端口安全(限制用户私自接入傻瓜交换机或非法主机接入)
七张图,学会做有价值的经营分析
Rock PI Development Notes (II): start with rock PI 4B plus (based on Ruixing micro rk3399) board and make system operation
Seven charts, learn to do valuable business analysis
Global and Chinese markets for carbon dioxide laser cutting heads 2022-2028: Research Report on technology, participants, trends, market size and share
大廠面試總結大全
Résumé de l'entrevue de Dachang Daquan
Privacy computing technology innovation and industry practice seminar: Learning
远程办公对我们的各方面影响心得 | 社区征文
[North Asia data recovery] data recovery case of raid crash caused by hard disk disconnection during data synchronization of hot spare disk of RAID5 disk array
OpenPose的使用
Error when uploading code to remote warehouse: remote origin already exists
基于多元时间序列对高考预测分析案例