当前位置:网站首页>LeetCode 871. Minimum refueling times
LeetCode 871. Minimum refueling times
2022-07-04 20:40:00 【JIeJaitt】
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 .
Example 1:
Input :target = 1, startFuel = 1, stations = [] Output :0 explain : We can get to our destination without refueling .
Example 2:
Input :target = 100, startFuel = 1, stations = [[10,100]] Output :-1 explain : We can't get to our destination , Not even getting to the first gas station .
Example 3:
Input :target = 100, startFuel = 10, stations = [[10,60],[20,30],[30,30],[60,40]] Output :2 explain : We set out with 10 Liter fuel . We drove to the starting point 10 The gas station at mile , Consume 10 Liter fuel . Take the gas from 0 To add to 60 l . then , We from 10 The gas station is miles away 60 The gas station at mile ( Consume 50 Liter fuel ), And take the gas from 10 To add to 50 l . Then we drove to our destination . We are on the way 1 Two gas stations stop , So back 2 .
Tips :
1 <= target, startFuel, stations[i][1] <= 10^9
0 <= stations.length <= 500
0 < stations[0][0] < stations[1][0] < ... < stations[stations.length-1][0] < target
Greedy question , It's difficult to prove , But the idea is more direct .
The specific methods are as follows .
Calculate the current position ( Gas station or destination ) The distance from the previous position , According to the distance difference, the amount of gasoline needed to drive from the previous position to the current position , Subtract the amount of gasoline used from the remaining amount of gasoline .
If the remaining gasoline quantity is less than 000, It means that you cannot drive from the previous position to the current position without refueling , We need to refuel . Take the largest element in the priority queue and add it to the remaining gasoline , And increase the refueling times 111, Repeat this operation until the remaining gasoline quantity is greater than or equal to 000 Or the priority queue becomes empty .
If the priority queue becomes empty , The remaining amount of gasoline is still less than 000, It means that you still can't reach the current position after refueling at all the passing gas stations , return −1-1−1.
If the current location is a gas station , Then add the refueling volume of the current gas station to the priority queue , And update the previous location with the current location .
class Solution {
public:
int minRefuelStops(int target, int startFuel, vector<vector<int>>& stations) {
stations.push_back({
target,0});
int res=0;
priority_queue<int> heap;
for (auto& p:stations) {
int x=p[0],y=p[1];
while(heap.size() && startFuel<x) {
startFuel += heap.top();
heap.pop();
res ++;
}
if (startFuel<x) return -1;
heap.push(y);
}
return res;
}
};
边栏推荐
- How to adapt your games to different sizes of mobile screen
- [ismb2022 tutorial] the picture shows the precision medicine of learning. Marinka zitnik, Harvard University, keynote speaker, with 87 ppt
- What are the consequences of closing the read / write channel?
- Optimization cases of complex factor calculation: deep imbalance, buying and selling pressure index, volatility calculation
- 如何让你的小游戏适配不同尺寸的手机屏幕
- Installation and use of VMware Tools and open VM tools: solve the problems of incomplete screen and unable to transfer files of virtual machines
- Thinking on demand development
- Anhui Zhong'an online culture and tourism channel launched a series of financial media products of "follow the small editor to visit Anhui"
- Process of manually encrypt the mass-producing firmware and programming ESP devices
- Integritee通过XCM集成至Moonriver,为其生态系统带来企业级隐私解决方案
猜你喜欢
Practical examples of node strong cache and negotiation cache
In the first month of its launch, the tourist praise rate of this campsite was as high as 99.9%! How did he do it?
QT writing the Internet of things management platform 38- multiple database support
What is involution?
【ISMB2022教程】图表示学习的精准医疗,哈佛大学Marinka Zitnik主讲,附87页ppt
Flet教程之 04 FilledTonalButton基础入门(教程含源码)
剑指 Offer II 80-100(持续更新)
Decryption function calculates "task state and lifecycle management" of asynchronous task capability
AP8022开关电源小家电ACDC芯片离线式开关电源IC
YOLOv5s-ShuffleNetV2
随机推荐
In the first month of its launch, the tourist praise rate of this campsite was as high as 99.9%! How did he do it?
idea大小写快捷键
What is the development of block hash quiz game system? Hash quiz game system development (case mature)
Stack: how to realize the judgment of valid brackets?
LeetCode 871. 最低加油次数
同事的接口文档我每次看着就头大,毛病多多。。。
凌云出海记 | 一零跃动&华为云:共助非洲普惠金融服务
电脑怎么保存网页到桌面上使用
[Beijing Xunwei] i.mx6ull development board porting Debian file system
Managed service network: application architecture evolution in the cloud native Era
Flet教程之 04 FilledTonalButton基础入门(教程含源码)
Anhui Zhong'an online culture and tourism channel launched a series of financial media products of "follow the small editor to visit Anhui"
【历史上的今天】7 月 4 日:第一本电子书问世;磁条卡的发明者出生;掌上电脑先驱诞生
Dynamic memory management
BFC面试简述
Flet教程之 06 TextButton基础入门(教程含源码)
Huawei Nova 10 series supports the application security detection function to build a strong mobile security firewall
Win11U盘拒绝访问怎么办?Win11U盘拒绝访问的有效解决方法
Employment prospects of neural network Internet of things application technology [welcome to add]
Pointnet / pointnet++ point cloud data set processing and training