当前位置:网站首页>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
边栏推荐
- MySQL高级(进阶)SQL语句
- 虚拟机初始化脚本, 虚拟机相互免秘钥
- Excel finds the same value in a column, deletes the row or replaces it with a blank value
- QT中的QPropertyAnimation使用和toast案列
- Memory management of C
- metric_logger小解
- Gamefi chain game system development (NFT chain game development function) NFT chain game system development (gamefi chain game development source code)
- 潇洒郎:彻底解决Markdown图片问题——无需上传图片——无需网络——转发给他人图片无缺失
- ICDE 2023|TKDE Poster Session(CFP)
- Learn the knowledge points of eight part essay ~ ~ 1
猜你喜欢

Web2.0的巨头纷纷布局VC,Tiger DAO VC或成抵达Web3捷径

Compile oglpg-9th-edition source code with clion

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

Introduction to the paper | application of machine learning in database cardinality estimation

新手必看,点击两个按钮切换至不同的内容

PHP-Parser羽毛球预约小程序开发require线上系统

Excel查找一列中的相同值,删除该行或替换为空值

论文导读 | 机器学习在数据库基数估计中的应用
![[100 cases of JVM tuning practice] 02 - five cases of virtual machine stack and local method stack tuning](/img/59/6c776e0607a52962b72fbea2e64c8e.png)
[100 cases of JVM tuning practice] 02 - five cases of virtual machine stack and local method stack tuning

PyTorch函数中的__call__和forward函数
随机推荐
Gamefi链游系统开发(NFT链游开发功能)丨NFT链游系统开发(Gamefi链游开发源码)
数据降维——主成分分析
Excel finds the same value in a column, deletes the row or replaces it with a blank value
Npoi export Excel2007
A4988 drive stepper motor "recommended collection"
4274. 后缀表达式-二叉表达式树
潇洒郎:彻底解决Markdown图片问题——无需上传图片——无需网络——转发给他人图片无缺失
Windows2008R2 安装 PHP7.4.30 必须 LocalSystem 启动应用程序池 不然500错误 FastCGI 进程意外退出
When converting from list to map, if a certain attribute may cause key duplication and exceptions, you can set the way to deal with this duplication
论文导读 | 机器学习在数据库基数估计中的应用
Reading notes of code neatness
Fastdfs installation
虚拟机初始化脚本, 虚拟机相互免秘钥
Microservice technology - distributed global ID in high concurrency
Qpropertyanimation use and toast case list in QT
The difference between interceptor and filter
Web2.0的巨头纷纷布局VC,Tiger DAO VC或成抵达Web3捷径
Why should we build an enterprise fixed asset management system and how can enterprises strengthen fixed asset management
IDEA编辑器去掉sql语句背景颜色SQL语句警告No data sources are configured to run this SQL...和SQL Dialect is Not Config
C file input operation