当前位置:网站首页>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;
}
};
边栏推荐
- 【日常训练】871. 最低加油次数
- 百度智能云牵头打造智能云综合标准化平台
- 【luogu P4320】道路相遇(圆方树)
- 2022 list of manufacturers of Chinese 3D vision enterprises (guided positioning and sorting scenes)
- Liad: the consumer end of micro LED products is first targeted at TVs above 100 inches. At this stage, it is still difficult to enter a smaller size
- Problèmes de configuration lex & yacc & Bison & Flex
- 线程的启动与优先级
- AttributeError: ‘tuple‘ object has no attribute ‘layer‘问题解决
- 机器学习:numpy版本线性回归预测波士顿房价
- Some introduction and precautions about XML
猜你喜欢
The "2022 China Digital Office Market Research Report" can be downloaded to explain the 176.8 billion yuan market in detail
Kubernetes resource object introduction and common commands (V) - (NFS & PV & PVC)
指针进阶(一)
AttributeError: ‘tuple‘ object has no attribute ‘layer‘问题解决
University of Toronto: Anthony coach | the conditions of deep reinforcement learning can induce dynamic risk measurement
[MCU project training] eight way answering machine
Linux软件:如何安装Redis服务
深度剖析数据在内存中的存储
File operation io-part2
Rust字符串切片、结构体和枚举类
随机推荐
Cordova plugin device obtains the device information plug-in, which causes Huawei to fail the audit
奥斯陆大学:Li Meng | 基于Swin-Transformer的深度强化学习
【雅思阅读】王希伟阅读P2(阅读填空)
About qbytearray storage hexadecimal and hexadecimal conversion
【雅思阅读】王希伟阅读P1(阅读判断题)
kubernetes编写yml简单入门
【AutoSAR 五 方法论】
leetcode-1964:找出到每个位置为止最长的有效障碍赛跑路线
Callback event after the antv X6 node is dragged onto the canvas (stepping on a big hole record)
Vulkan-性能及精细化
[IELTS reading] Wang Xiwei reading P1 (reading judgment question)
【AutoSAR 四 BSW概述】
Nc17059 queue Q
文件操作IO-Part2
lex && yacc && bison && flex 配置的問題
Rust所有权(非常重要)
Nc50528 sliding window
关于QByteArray存储十六进制 与十六进制互转
node_modules删不掉
[jetcache] jetcache configuration description and annotation attribute description