当前位置:网站首页>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;
}
};
边栏推荐
- john爆破出现Using default input encoding: UTF-8 Loaded 1 password hash (bcrypt [Blowfish 32/64 X3])
- 深度之眼(三)——矩阵的行列式
- 默认浏览器设置不了怎么办?
- LeetCode 2. Add two numbers
- Linux Installation PostgreSQL + Patroni cluster problem
- Hard core! One configuration center for 8 classes!
- P6774 [NOI2020] 时代的眼泪(分块)
- 远程办公对我们的各方面影响心得 | 社区征文
- Connect Porsche and 3PL EDI cases
- John blasting appears using default input encoding: UTF-8 loaded 1 password hash (bcrypt [blowfish 32/64 x3])
猜你喜欢
随机推荐
Linux Installation PostgreSQL + Patroni cluster problem
Weili holdings listed on the Hong Kong Stock Exchange: with a market value of HK $500million, it contributed an IPO to Hubei
Notice on holding a salon for young editors of scientific and Technological Journals -- the abilities and promotion strategies that young editors should have in the new era
PhD battle-11 preview | review and prospect backdoor attack and defense of neural network
绿竹生物冲刺港股:年期内亏损超5亿 泰格医药与北京亦庄是股东
宝宝巴士创业板IPO被终止:曾拟募资18亿 唐光宇控制47%股权
Cell: Tsinghua Chenggong group revealed an odor of skin flora. Volatiles promote flavivirus to infect the host and attract mosquitoes
七张图,学会做有价值的经营分析
基于多元时间序列对高考预测分析案例
Green bamboo biological sprint Hong Kong stocks: loss of more than 500million during the year, tiger medicine and Beijing Yizhuang are shareholders
亚马逊云科技 Community Builder 申请窗口开启
Seven charts, learn to do valuable business analysis
&lt;四&gt; H264解码输出yuv文件
R and rstudio download and installation tutorial (super detailed)
MOSFET器件手册关键参数解读
MySQL port
Kubernetes three open interfaces first sight
剑指 Offer 21. 调整数组顺序使奇数位于偶数前面
IP地址转换地址段
Use of openpose







