当前位置:网站首页>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^90 <= stations.length <= 5000 < 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;
}
};
边栏推荐
- Aiming at the "amnesia" of deep learning, scientists proposed that based on similarity weighted interleaved learning, they can board PNAS
- 浏览器渲染页面过程
- 泰山OFFICE技术讲座:关于背景(底纹和高亮)的顺序问题
- GVM使用
- 六石编程学:关于代码,有六个得意
- 凌云出海记 | 沐融科技&华为云:打造非洲金融SaaS解决方案样板
- 太方便了,钉钉上就可完成代码发布审批啦!
- Lingyun going to sea | Murong Technology & Huawei cloud: creating a model of financial SaaS solutions in Africa
- 科普达人丨一文看懂阿里云的秘密武器“神龙架构”
- repeat_ P1002 [NOIP2002 popularization group] cross the river pawn_ dp
猜你喜欢

【深度学习】一文看尽Pytorch之十九种损失函数

Key rendering paths for performance optimization

Decryption function calculates "task state and lifecycle management" of asynchronous task capability

Pointnet / pointnet++ point cloud data set processing and training

Common verification rules of form components -1 (continuously updating ~)
实践示例理解js强缓存协商缓存

FS4061A升压8.4V充电IC芯片和FS4061B升压12.6V充电IC芯片规格书datasheet

Cann operator: using iterators to efficiently realize tensor data cutting and blocking processing
![[today in history] July 4: the first e-book came out; The inventor of magnetic stripe card was born; Palm computer pioneer was born](/img/0b/73f0d98a6db813e54074abe199ed98.png)
[today in history] July 4: the first e-book came out; The inventor of magnetic stripe card was born; Palm computer pioneer was born

九齐NY8B062D MCU规格书/datasheet
随机推荐
Write it down once Net analysis of thread burst height of an industrial control data acquisition platform
1500万员工轻松管理,云原生数据库GaussDB让HR办公更高效
B2B mall system development of electronic components: an example of enabling enterprises to build standardized purchase, sale and inventory processes
idea恢复默认快捷键
Data set division
Informatics Olympiad 1336: [example 3-1] find roots and children
Talking about cookies of client storage technology
二叉树的四种遍历方式以及中序后序、前序中序、前序后序、层序创建二叉树【专为力扣刷题而打造】
Flet教程之 07 PopupMenuButton基础入门(教程含源码)
六石编程学:关于代码,有六个得意
Employment prospects of neural network Internet of things application technology [welcome to add]
被奉为经典的「金字塔原理」,教给我们哪些PPT写作技巧?
左右最值最大差问题
哈希(Hash)竞猜游戏系统开发功能分析及源码
为什么最大速度是光速
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?
Practical examples of node strong cache and negotiation cache
idea配置标准注释
2022 version of stronger jsonpath compatibility and performance test (snack3, fastjson2, jayway.jsonpath)
Is it necessary to apply for code signing certificate for software client digital signature?