当前位置:网站首页>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;
}
};
边栏推荐
- 关于XML一些介绍和注意事项
- Vulkan practice first bullet
- 【案例分享】让新时代教育发展与“数”俱进
- [jetcache] jetcache configuration description and annotation attribute description
- Kubernetes resource object introduction and common commands (V) - (NFS & PV & PVC)
- University of Toronto: Anthony coach | the conditions of deep reinforcement learning can induce dynamic risk measurement
- Attributeerror: 'tuple' object has no attribute 'layer' problem solving
- MySQL 23 classic interview hanging interviewer
- Introduction of UART, RS232, RS485, I2C and SPI
- Wechat applet obtains the information of an element (height, width, etc.) and converts PX to rpx.
猜你喜欢
![[MCU project training] eight way answering machine](/img/a3/6a50619cd16269bf485a4a273677aa.jpg)
[MCU project training] eight way answering machine

指针初阶(基础)

MySQL 23 classic interview hanging interviewer

1.12 - 指令

logback配置文件

Pageoffice - bug modification journey
![[shutter] image component (the placeholder | transparent_image transparent image plug-in is loaded into the memory)](/img/73/19e2e0fc5ea6f05e34584ba40a452d.jpg)
[shutter] image component (the placeholder | transparent_image transparent image plug-in is loaded into the memory)

【AutoSAR 十三 NVM】

Solution to the problem of abnormal display of PDF exported Chinese documents of confluence

antv x6节点拖拽到画布上后的回调事件(踩大坑记录)
随机推荐
File operation io-part2
机器学习:numpy版本线性回归预测波士顿房价
The "2022 China Digital Office Market Research Report" can be downloaded to explain the 176.8 billion yuan market in detail
About the practice topic of screen related to unity screen, unity moves around a certain point inside
Arduino开发之按键检测与正弦信号输出
【AutoSAR 六 描述文件】
Nc20806 District interval
Web2.0 giants have deployed VC, and tiger Dao VC may become a shortcut to Web3
数组与集合性能比较
【AutoSAR 九 C/S原理架构】
logback配置文件
In the first half of 2022, there are 10 worth seeing, and each sentence can bring you strength!
Automated defect analysis in electron microscopic images-论文阅读笔记
FAQ | FAQ for building applications for large screen devices
Shell implements basic file operations (cutting, sorting, and de duplication)
Web2.0的巨头纷纷布局VC,Tiger DAO VC或成抵达Web3捷径
leetcode-871:最低加油次数
Solution to the problem of abnormal display of PDF exported Chinese documents of confluence
Illustrated network: what is virtual router redundancy protocol VRRP?
【AutoSAR 十三 NVM】