当前位置:网站首页>LeetCode 0871.最低加油次数 - 类似于POJ2431丛林探险
LeetCode 0871.最低加油次数 - 类似于POJ2431丛林探险
2022-07-02 18:19:00 【Tisfy】
【LetMeFly】871.最低加油次数 - 类似于POJ2431丛林探险
力扣题目链接:https://leetcode.cn/problems/minimum-number-of-refueling-stops/
汽车从起点出发驶向目的地,该目的地位于出发位置东面 target
英里处。
沿途有加油站,每个 station[i]
代表一个加油站,它位于出发位置东面 station[i][0]
英里处,并且有 station[i][1]
升汽油。
假设汽车油箱的容量是无限的,其中最初有 startFuel
升燃料。它每行驶 1 英里就会用掉 1 升汽油。
当汽车到达加油站时,它可能停下来加油,将所有汽油从加油站转移到汽车中。
为了到达目的地,汽车所必要的最低加油次数是多少?如果无法到达目的地,则返回 -1
。
注意:如果汽车到达加油站时剩余燃料为 0,它仍然可以在那里加油。如果汽车到达目的地时剩余燃料为 0,仍然认为它已经到达目的地。
示例 1:
输入:target = 1, startFuel = 1, stations = [] 输出:0 解释:我们可以在不加油的情况下到达目的地。
示例 2:
输入:target = 100, startFuel = 1, stations = [[10,100]] 输出:-1 解释:我们无法抵达目的地,甚至无法到达第一个加油站。
示例 3:
输入:target = 100, startFuel = 10, stations = [[10,60],[20,30],[30,30],[60,40]] 输出:2 解释: 我们出发时有 10 升燃料。 我们开车来到距起点 10 英里处的加油站,消耗 10 升燃料。将汽油从 0 升加到 60 升。 然后,我们从 10 英里处的加油站开到 60 英里处的加油站(消耗 50 升燃料), 并将汽油从 10 升加到 50 升。然后我们开车抵达目的地。 我们沿途在1两个加油站停靠,所以返回 2 。
提示:
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
方法一:贪心 + 优先队列
这道题让人很容易想到递归。
这道题可参考题解丛林探险
方法也很简单:
若在可用到达的距离范围内有多个加油站,则将这些加油站点的加油量入队(优先队列)。
若走到下一个加油站之前油会耗尽,则需要加油(优先队列中最大的加油量)后继续走。
当油量大于等于卡车到城镇的距离L时结束。
- 时间复杂度 O ( n log n ) O(n\log n) O(nlogn),其中 n n n是加油站个数
- 空间复杂度 O ( n ) O(n) O(n)
AC代码
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;
}
};
同步发文于CSDN,原创不易,转载请附上原文链接哦~
Tisfy:https://letmefly.blog.csdn.net/article/details/125575683
边栏推荐
- 9D电影是怎样的?(+维度空间常识)
- R language uses the lsnofunction function function of epidisplay package to list all objects in the current space, except user-defined function objects
- Novice must see, click two buttons to switch to different content
- mybatiesHelperPro工具必须的可以生成到对应项目文件夹下
- [0701] [paper reading] allowing data imbalance issue with perforated input during influence
- PHP-Parser羽毛球预约小程序开发require线上系统
- Have you stepped on the nine common pits in the e-commerce system?
- 使用 Cheat Engine 修改 Kingdom Rush 中的金钱、生命、星
- [论文阅读] CA-Net: Leveraging Contextual Features for Lung Cancer Prediction
- R language dplyr package filter function filters dataframe data. If the name of the data column (variable) to be filtered contains quotation marks, you need to use!! SYM syntax processing, otherwise n
猜你喜欢
What is 9D movie like? (+ common sense of dimension space)
[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
教程篇(5.0) 10. 故障排除 * FortiEDR * Fortinet 網絡安全專家 NSE 5
Processing strategy of message queue message loss and repeated message sending
Imitation Jingdong magnifying glass effect (pink teacher version)
juypter notebook 修改默认打开文件夹以及默认浏览器
[100 cases of JVM tuning practice] 02 - five cases of virtual machine stack and local method stack tuning
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
随机推荐
R language dplyr package filter function filters dataframe data. If the name of the data column (variable) to be filtered contains quotation marks, you need to use!! SYM syntax processing, otherwise n
Preprocessing and preprocessing macros
9D电影是怎样的?(+维度空间常识)
使用CLion编译OGLPG-9th-Edition源码
Codeworks 5 questions per day (1700 average) - day 4
Date tool class (updated from time to time)
What is the MySQL backup suffix_ MySQL backup restore
Tutorial (5.0) 10 Troubleshooting * fortiedr * Fortinet network security expert NSE 5
[100 cases of JVM tuning practice] 03 -- four cases of JVM heap tuning
高级性能测试系列《24. 通过jdbc执行sql脚本》
PHP asymmetric encryption method private key and public key encryption and decryption method
性能测试如何创造业务价值
Mysql高级篇学习总结6:索引的概念及理解、B+树产生过程详解、MyISAM与InnoDB的对比
FastDFS安装
yolov3 训练自己的数据集之生成train.txt
Virtual machine initialization script, virtual machine mutual secret key free
Gstore weekly gstore source code analysis (4): black and white list configuration analysis of security mechanism
为什么要做企业固定资产管理系统,企业如何加强固定资产管理
开发固定资产管理系统,开发固定资产管理系统用什么语音
Novice must see, click two buttons to switch to different content