当前位置:网站首页>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^90 <= stations.length <= 5000 < 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
边栏推荐
- Stm32g0 USB DFU upgrade verification error -2
- [0701] [paper reading] allowing data imbalance issue with perforated input during influence
- Golang concurrent programming goroutine, channel, sync
- 开发固定资产管理系统,开发固定资产管理系统用什么语音
- Digital scroll strip animation
- How to print mybats log plug-in using XML file
- Why should we build an enterprise fixed asset management system and how can enterprises strengthen fixed asset management
- PHP parser badminton reservation applet development requires online system
- 机器学习笔记 - 时间序列预测研究:法国香槟的月销量
- C的内存管理
猜你喜欢
![[100 cases of JVM tuning practice] 01 - introduction of JVM and program counter](/img/c4/3bba96fda92328704c2ddd929dcdf6.png)
[100 cases of JVM tuning practice] 01 - introduction of JVM and program counter

IEDA refactor的用法

In pytorch function__ call__ And forward functions

线程应用实例

新手必看,點擊兩個按鈕切換至不同的內容

How performance testing creates business value

思维意识转变是施工企业数字化转型成败的关键

【测试开发】软件测试—概念篇

Juypter notebook modify the default open folder and default browser

Codeworks 5 questions per day (1700 average) - day 4
随机推荐
守望先锋世界观架构 ——(一款好的游戏是怎么来的)
2022编译原理期末考试 回忆版
Excel查找一列中的相同值,删除该行或替换为空值
C文件输入操作
MySQL advanced (Advanced) SQL statement
注解开发方式下AutowiredAnnotationBeanPostProcessor的注册时机
【测试开发】软件测试—概念篇
教程篇(5.0) 10. 故障排除 * FortiEDR * Fortinet 网络安全专家 NSE 5
How to play when you travel to Bangkok for the first time? Please keep this money saving strategy
Processing strategy of message queue message loss and repeated message sending
How can retail enterprises open the second growth curve under the full link digital transformation
使用CLion编译OGLPG-9th-Edition源码
What is the MySQL backup suffix_ MySQL backup restore
How to print mybats log plug-in using XML file
R language uses the lsnofunction function function of epidisplay package to list all objects in the current space, except user-defined function objects
reduce--遍历元素计算 具体的计算公式需要传入 结合BigDecimal
Preprocessing and preprocessing macros
Golang并发编程——goroutine、channel、sync
Digital scroll strip animation
性能测试如何创造业务价值