当前位置:网站首页>LeetCode 0871. Minimum refueling times - similar to poj2431 jungle adventure
LeetCode 0871. Minimum refueling times - similar to poj2431 jungle adventure
2022-07-02 19:28:00 【Tisfy】
【LetMeFly】871. Minimum refueling times - Be similar to POJ2431 Jungle exploration
Force button topic link :https://leetcode.cn/problems/minimum-number-of-refueling-stops/
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
Method 1 : greedy + Priority queue
This problem makes it easy to think of recursion .
This problem can be solved by reference Jungle exploration
And it's very simple :
If there are multiple gas stations within the available distance , Then join the refueling volume of these refueling stations ( Priority queue ).
If you walk to the next gas station, the oil will run out , You need to refuel ( The largest refueling volume in the priority queue ) Keep walking after .
When the fuel volume is greater than or equal to the distance from the truck to the town L End of time .
- Time complexity O ( n log n ) O(n\log n) O(nlogn), among n n n It's the number of gas stations
- Spatial complexity O ( n ) O(n) O(n)
AC Code
C++
class Solution {
public:
int minRefuelStops(int target, int startFuel, vector<vector<int>>& stations) {
sort(stations.begin(), stations.end(), [](vector<int>& a, vector<int>& b) {
return a[0] < b[0];
});
int ans = 0;
int canGo = startFuel;
int loc = 0;
priority_queue<int> pq;
while (canGo < target) {
while (loc < stations.size() && stations[loc][0] <= canGo) {
pq.push(stations[loc][1]);
loc++;
}
if (pq.empty())
return -1;
canGo += pq.top();
pq.pop();
ans++;
}
return ans;
}
};
Synchronous posting on CSDN, Originality is not easy. , Reprint please attach Link to the original text Oh ~
Tisfy:https://letmefly.blog.csdn.net/article/details/125575683
边栏推荐
- codeforces每日5题(均1700)-第四天
- mybatiesHelperPro工具必须的可以生成到对应项目文件夹下
- Watchful pioneer world outlook Architecture - (how does a good game come from)
- 高级性能测试系列《24. 通过jdbc执行sql脚本》
- Compile oglpg-9th-edition source code with clion
- ICDE 2023|TKDE Poster Session(CFP)
- [论文阅读] CA-Net: Leveraging Contextual Features for Lung Cancer Prediction
- Tutoriel (5.0) 10. Dépannage * fortiedr * fortinet Network Security expert NSE 5
- C文件输入操作
- 仿京东放大镜效果(pink老师版)
猜你喜欢
![[0701] [论文阅读] Alleviating Data Imbalance Issue with Perturbed Input During Inference](/img/c7/9b7dc4b4bda4ecfe07aec1367fe059.png)
[0701] [论文阅读] Alleviating Data Imbalance Issue with Perturbed Input During Inference

Transformation of thinking consciousness is the key to the success or failure of digital transformation of construction enterprises

Processing strategy of message queue message loss and repeated message sending

Learning summary of MySQL advanced 6: concept and understanding of index, detailed explanation of b+ tree generation process, comparison between MyISAM and InnoDB

IDEA编辑器去掉sql语句背景颜色SQL语句警告No data sources are configured to run this SQL...和SQL Dialect is Not Config

守望先锋世界观架构 ——(一款好的游戏是怎么来的)

数据降维——主成分分析

PHP parser badminton reservation applet development requires online system
![[error record] problems related to the installation of the shuttle environment (follow-up error handling after executing the shuttle doctor command)](/img/c1/a00425f2e1824a50644c8fbb15fe38.jpg)
[error record] problems related to the installation of the shuttle environment (follow-up error handling after executing the shuttle doctor command)

Advanced performance test series "24. Execute SQL script through JDBC"
随机推荐
Progress progress bar
Juypter notebook modify the default open folder and default browser
A4988 drive stepper motor "recommended collection"
Golang并发编程——goroutine、channel、sync
Which video recording software is better for the computer
Horizontal ultra vires and vertical ultra vires [easy to understand]
[论文阅读] CA-Net: Leveraging Contextual Features for Lung Cancer Prediction
Golang concurrent programming goroutine, channel, sync
思维意识转变是施工企业数字化转型成败的关键
使用 Cheat Engine 修改 Kingdom Rush 中的金钱、生命、星
Why should we build an enterprise fixed asset management system and how can enterprises strengthen fixed asset management
Mobile robot path planning: artificial potential field method [easy to understand]
Gstore weekly gstore source code analysis (4): black and white list configuration analysis of security mechanism
Refactoring: improving the design of existing code (Part 1)
《病人家属,请来一下》读书笔记
End to end object detection with transformers (Detr) paper reading and understanding
[100 cases of JVM tuning practice] 01 - introduction of JVM and program counter
C文件输入操作
IEDA refactor的用法
ORA-01455: converting column overflows integer datatype