当前位置:网站首页>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;
}
};
边栏推荐
- Exploration and practice of integration of streaming and wholesale in jd.com
- Yolov5 practice: teach object detection by hand
- Atcoder beginer contest 169 (B, C, D unique decomposition, e mathematical analysis f (DP))
- Global and Chinese markets for carbon dioxide laser cutting heads 2022-2028: Research Report on technology, participants, trends, market size and share
- unity Hub 登錄框變得很窄 無法登錄
- Go zero micro service practical series (VIII. How to handle tens of thousands of order requests per second)
- 2322. 从树中删除边的最小分数(异或和&模拟)
- john爆破出現Using default input encoding: UTF-8 Loaded 1 password hash (bcrypt [Blowfish 32/64 X3])
- 伟立控股港交所上市:市值5亿港元 为湖北贡献一个IPO
- 小鹏P7雨天出事故安全气囊没有弹出 官方回应:撞击力度未达到弹出要求
猜你喜欢
GeoServer:发布PostGIS数据源
[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
What is normal distribution? What is the 28 law?
易语言abcd排序
相信自己,这次一把搞定JVM面试
电脑自带软件使图片底色变为透明(抠图白底)
大厂面试总结大全
社交元宇宙平台Soul冲刺港股:年营收12.8亿 腾讯是股东
基于多元时间序列对高考预测分析案例
Download blender on Alibaba cloud image station
随机推荐
Amazon cloud technology community builder application window opens
Machine learning perceptron model
Role and function of uboot
LeetCode 2. 两数相加
go-zero微服务实战系列(八、如何处理每秒上万次的下单请求)
【征文活动】亲爱的开发者,RT-Thread社区喊你投稿啦
Seven charts, learn to do valuable business analysis
&lt; IV & gt; H264 decode output YUV file
LeetCode 1. Sum of two numbers
串口控制舵机转动
有赞和腾讯云、阿里云一同摘得“中国企业云科技服务商50强”[通俗易懂]
MOSFET器件手册关键参数解读
TCP congestion control details | 2 background
LeetCode 4. Find the median (hard) of two positive arrays
Interpretation of key parameters in MOSFET device manual
[essay solicitation activity] Dear developer, RT thread community calls you to contribute
远程办公对我们的各方面影响心得 | 社区征文
MySQL port
对接保时捷及3PL EDI案例
[error record] the connection of the flutter device shows loading (disconnect | delete the shuttle/bin/cache/lockfile file)