当前位置:网站首页>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
边栏推荐
- [论文阅读] CA-Net: Leveraging Contextual Features for Lung Cancer Prediction
- Mobile robot path planning: artificial potential field method [easy to understand]
- End-to-End Object Detection with Transformers(DETR)论文阅读与理解
- Novice must see, click two buttons to switch to different content
- golang:[]byte转string
- 从list转化成map的时候,如果根据某一属性可能会导致key重复而异常,可以设置处理这种重复的方式
- 《病人家属,请来一下》读书笔记
- Quanzhi A33 uses mainline u-boot
- The R language dplyr package rowwise function and mutate function calculate the maximum value of multiple data columns in each row in the dataframe data, and generate the data column (row maximum) cor
- 教程篇(5.0) 10. 故障排除 * FortiEDR * Fortinet 網絡安全專家 NSE 5
猜你喜欢
Tutorial (5.0) 09 Restful API * fortiedr * Fortinet network security expert NSE 5
教程篇(5.0) 10. 故障排除 * FortiEDR * Fortinet 网络安全专家 NSE 5
性能测试如何创造业务价值
论文导读 | 机器学习在数据库基数估计中的应用
Excel finds the same value in a column, deletes the row or replaces it with a blank value
How can retail enterprises open the second growth curve under the full link digital transformation
Yolov3 trains its own data set to generate train txt
ICDE 2023|TKDE Poster Session(CFP)
线程应用实例
Data dimensionality reduction principal component analysis
随机推荐
NPOI导出Excel2007
Web2.0 giants have deployed VC, and tiger Dao VC may become a shortcut to Web3
Learning summary of MySQL advanced 6: concept and understanding of index, detailed explanation of b+ tree generation process, comparison between MyISAM and InnoDB
The difference between interceptor and filter
Fastdfs installation
高级性能测试系列《24. 通过jdbc执行sql脚本》
What is the MySQL backup suffix_ MySQL backup restore
MySQL高级(进阶)SQL语句
数据降维——因子分析
Imitation Jingdong magnifying glass effect (pink teacher version)
Memory management of C
What is 9D movie like? (+ common sense of dimension space)
机器学习笔记 - 时间序列预测研究:法国香槟的月销量
守望先锋世界观架构 ——(一款好的游戏是怎么来的)
Quanzhi A33 uses mainline u-boot
MySQL advanced (Advanced) SQL statement
【测试开发】一文带你了解什么是软件测试
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
横向越权与纵向越权[通俗易懂]
[100 cases of JVM tuning practice] 03 -- four cases of JVM heap tuning