当前位置:网站首页>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;
}
};
边栏推荐
- Leetcode1380: lucky numbers in matrix
- Method of C language self defining function
- ⌈ 2022 ⌋ how to use webp gracefully in projects
- TCP congestion control details | 2 background
- 基于多元时间序列对高考预测分析案例
- john爆破出現Using default input encoding: UTF-8 Loaded 1 password hash (bcrypt [Blowfish 32/64 X3])
- Use of openpose
- 国内比较好的OJ平台[通俗易懂]
- Rock PI Development Notes (II): start with rock PI 4B plus (based on Ruixing micro rk3399) board and make system operation
- A week of short video platform 30W exposure, small magic push helps physical businesses turn losses into profits
猜你喜欢

隐私计算技术创新及产业实践研讨会:学习

How openharmony starts FA of remote devices

Exploration and practice of integration of streaming and wholesale in jd.com

PhD battle-11 preview | review and prospect backdoor attack and defense of neural network

移动应用性能工具探索之路

PWM控制舵机

綠竹生物沖刺港股:年期內虧損超5億 泰格醫藥與北京亦莊是股東

寒门再出贵子:江西穷县考出了省状元,做对了什么?
![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])

机器学习-感知机模型
随机推荐
Method of C language self defining function
A week of short video platform 30W exposure, small magic push helps physical businesses turn losses into profits
Résumé de l'entrevue de Dachang Daquan
PWM控制舵机
La boîte de connexion du hub de l'unit é devient trop étroite pour se connecter
Understand one article: four types of data index system
How to choose the right kubernetes storage plug-in? (09)
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
Global and Chinese markets for carbon dioxide laser cutting heads 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])
远程办公对我们的各方面影响心得 | 社区征文
Linux Installation PostgreSQL + Patroni cluster problem
go-zero微服务实战系列(八、如何处理每秒上万次的下单请求)
LeetCode 2. Add two numbers
PWM controlled steering gear
Youzan won the "top 50 Chinese enterprise cloud technology service providers" together with Tencent cloud and Alibaba cloud [easy to understand]
Global and Chinese markets for slotting milling machines 2022-2028: Research Report on technology, participants, trends, market size and share
Exploration and practice of integration of streaming and wholesale in jd.com
uboot的作用和功能
Classic quotations