当前位置:网站首页>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;
}
};
边栏推荐
- lex && yacc && bison && flex 配置的问题
- Two common methods and steps of character device registration
- 1.12 - Instructions
- leetcode-2115:从给定原材料中找到所有可以做出的菜
- Tensorflow 2.x(keras)源码详解之第十五章:迁移学习与微调
- Multi process programming (III): message queue
- University of Toronto: Anthony coach | the conditions of deep reinforcement learning can induce dynamic risk measurement
- Multiprocess programming (V): semaphores
- 【AutoSAR 一 概述】
- Illustrated network: what is virtual router redundancy protocol VRRP?
猜你喜欢

【AutoSAR 一 概述】

【AutoSAR 四 BSW概述】

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

【小程序项目开发-- 京东商城】uni-app之自定义搜索组件(中)-- 搜索建议

Automated defect analysis in electron microscopic images-论文阅读笔记

【AutoSAR 六 描述文件】

In the first half of 2022, there are 10 worth seeing, and each sentence can bring you strength!

【AutoSAR 十二 模式管理】

2022中国3D视觉企业(引导定位、分拣场景)厂商名单

Multiprocess programming (II): Pipeline
随机推荐
【AutoSAR 十 IO架构】
【AutoSAR 八 OS】
AEM: Nanlin fan Ben et al. - plant rhizosphere growth promoting bacteria control soybean blight
University of Toronto: Anthony coach | the conditions of deep reinforcement learning can induce dynamic risk measurement
Shell 实现文件基本操作(切割、排序、去重)
【AutoSAR 五 方法论】
Shell implements basic file operations (cutting, sorting, and de duplication)
Nc17059 queue Q
【AutoSAR 一 概述】
The "2022 China Digital Office Market Research Report" can be downloaded to explain the 176.8 billion yuan market in detail
Vulkan performance and refinement
图解网络:什么是虚拟路由器冗余协议 VRRP?
[golang syntax] map common errors golang panic: assignment to entry in nil map
文件操作IO-Part2
【案例分享】让新时代教育发展与“数”俱进
Use Jenkins II job
One of the reasons why setinterval timer does not take effect in ie: the callback is the arrow function
How to find out the currently running version of Solr- How do I find out version of currently running Solr?
AttributeError: ‘tuple‘ object has no attribute ‘layer‘问题解决
【AutoSAR 九 C/S原理架构】