当前位置:网站首页>Leetcode-871: minimum refueling times
Leetcode-871: minimum refueling times
2022-07-03 00:47:00 【Chrysanthemum headed bat】
leetcode-871: Minimum refueling times
subject
The car set off from the starting point to the destination , The purpose is to the east of the starting position target Miles away .
There are gas stations along the way , Every station[i] For a gas station , It's east of the starting point station[i][0] Miles away , And there are station[i][1] A litre of petrol .
Suppose the capacity of the car's fuel tank is infinite , At first there was startFuel Liter fuel . Every time it drives 1 Miles will be used 1 A litre of petrol .
When the car arrives at the gas station , It may stop to refuel , Transfer all gasoline from the gas station to the car .
In order to reach the destination , What's the minimum number of refuels necessary for a car ? If you can't get to your destination , Then return to -1 .
Be careful : If the remaining fuel when the car arrives at the gas station is 0, It can still refuel there . If the remaining fuel is 0, Still think it has reached its destination .
Problem solving
Method 1 : greedy
This solution and leetcode-45: Jumping game II It's similar , Calculate the maximum coverage distance each time . How many times can you cover the end , It means several times .
class Solution {
public:
int minRefuelStops(int target, int startFuel, vector<vector<int>>& stations) {
priority_queue<int> q;
int res=0;
int i=0;
while(startFuel<target){
while(i<stations.size()&&startFuel>=stations[i][0]){
q.push(stations[i++][1]);
}
if(q.empty()) return -1;
startFuel+=q.top();
q.pop();
res++;
}
return res;
}
};
边栏推荐
- About qbytearray storage hexadecimal and hexadecimal conversion
- [golang syntax] map common errors golang panic: assignment to entry in nil map
- 图解网络:什么是虚拟路由器冗余协议 VRRP?
- Rust string slicing, structs, and enumeration classes
- Overlay of shutter (Pop-Up)
- [daily training] 871 Minimum refueling times
- 2022上半年值得被看见的10条文案,每一句都能带给你力量!
- Rust ownership (very important)
- Tensorflow 2.x(keras)源码详解之第十五章:迁移学习与微调
- 数组与集合性能比较
猜你喜欢

2022 list of manufacturers of Chinese 3D vision enterprises (guided positioning and sorting scenes)

leetcode-2280:表示一个折线图的最少线段数

tail -f 、tail -F、tailf的区别

使用jenkins之二Job

Linux Software: how to install redis service

1.11 - bus

One of the reasons why setinterval timer does not take effect in ie: the callback is the arrow function

FAQ | FAQ for building applications for large screen devices

Multiprocess programming (I): basic concepts

【小程序项目开发-- 京东商城】uni-app之自定义搜索组件(中)-- 搜索建议
随机推荐
Overlay of shutter (Pop-Up)
An excellent orm in dotnet circle -- FreeSQL
2022中国3D视觉企业(引导定位、分拣场景)厂商名单
NC50965 Largest Rectangle in a Histogram
Basic use of shell script
Meaning of Tencent cloud free SSL certificate extension file
Array common operation methods sorting (including ES6) and detailed use
【AutoSAR 六 描述文件】
Use Jenkins II job
Nc20806 District interval
【luogu P4320】道路相遇(圆方树)
University of Oslo: Li Meng | deep reinforcement learning based on swing transformer
Extension of flutter
可下载《2022年中国数字化办公市场研究报告》详解1768亿元市场
使用jenkins之二Job
【AutoSAR 七 工具链简介】
Shell implements basic file operations (cutting, sorting, and de duplication)
为什么网站打开速度慢?
[daily training] 871 Minimum refueling times
Vulkan-实践第一弹