当前位置:网站首页>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;
}
};
边栏推荐
- Domestic relatively good OJ platform [easy to understand]
- Penetration tool - intranet permission maintenance -cobalt strike
- 社交元宇宙平台Soul冲刺港股:年营收12.8亿 腾讯是股东
- Configure MySQL under Linux to authorize a user to access remotely, which is not restricted by IP
- Interpretation of key parameters in MOSFET device manual
- 亚马逊云科技 Community Builder 申请窗口开启
- Dgraph: large scale dynamic graph dataset
- Atcoder beginer contest 169 (B, C, D unique decomposition, e mathematical analysis f (DP))
- 深度之眼(三)——矩阵的行列式
- C语言中sprintf()函数的用法
猜你喜欢
PWM控制舵机
Green bamboo biological sprint Hong Kong stocks: loss of more than 500million during the year, tiger medicine and Beijing Yizhuang are shareholders
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
伟立控股港交所上市:市值5亿港元 为湖北贡献一个IPO
Go zero micro service practical series (VIII. How to handle tens of thousands of order requests per second)
[error record] error -32000 received from application: there are no running service protocol
The macrogenome microbiome knowledge you want is all here (2022.7)
L'explosion de John utilise l'encodage d'entrée par défaut: UTF - 8 Loaded 1 password Hash (bcrypt [blowfish 32 / 64 X3])
linux安装postgresql + patroni 集群问题
博客主题 “Text“ 夏日清新特别版
随机推荐
Tech talk activity preview | building intelligent visual products based on Amazon kVs
串口控制舵机转动
P6774 [noi2020] tears in the era (block)
[cloud native] briefly talk about the understanding of flume, a massive data collection component
QWebEngineView崩溃及替代方案
&lt; IV & gt; H264 decode output YUV file
Deep learning image data automatic annotation [easy to understand]
Linux Installation PostgreSQL + Patroni cluster problem
超卓航科上市:募资9亿市值超60亿 成襄阳首家科创板企业
System Verilog实现优先级仲裁器
PWM控制舵机
In MySQL and Oracle, the boundary and range of between and precautions when querying the date
Tech Talk 活动预告 | 基于Amazon KVS打造智能视觉产品
【征文活动】亲爱的开发者,RT-Thread社区喊你投稿啦
PWM breathing lamp
綠竹生物沖刺港股:年期內虧損超5億 泰格醫藥與北京亦莊是股東
Configure ARP table entry restrictions and port security based on the interface (restrict users' private access to fool switches or illegal host access)
基于多元时间序列对高考预测分析案例
【云原生】简单谈谈海量数据采集组件Flume的理解
【Leetcode】14. 最长公共前缀