当前位置:网站首页>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
边栏推荐
- 【ERP软件】ERP体系二次开发有哪些危险?
- 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
- Introduction to the paper | analysis and criticism of using the pre training language model as a knowledge base
- 以太网PHY层芯片LAN8720A简介
- MySQL advanced learning summary 7: MySQL data structure - Comparison of hash index, AVL tree, B tree and b+ tree
- mysql备份后缀是什么_mysql备份还原
- QT中的QPropertyAnimation使用和toast案列
- 数据降维——因子分析
- 2022.7.1-----leetcode.241
- juypter notebook 修改默认打开文件夹以及默认浏览器
猜你喜欢

使用 Cheat Engine 修改 Kingdom Rush 中的金钱、生命、星

Talk about the design of red envelope activities in e-commerce system

Use cheat engine to modify money, life and stars in Kingdom rush

Introduction to the paper | analysis and criticism of using the pre training language model as a knowledge base

Web2.0 giants have deployed VC, and tiger Dao VC may become a shortcut to Web3

Have you stepped on the nine common pits in the e-commerce system?

Markdown基础语法

How to play when you travel to Bangkok for the first time? Please keep this money saving strategy

Tutoriel (5.0) 10. Dépannage * fortiedr * fortinet Network Security expert NSE 5

新手必看,點擊兩個按鈕切換至不同的內容
随机推荐
Introduction to the paper | application of machine learning in database cardinality estimation
教程篇(5.0) 10. 故障排除 * FortiEDR * Fortinet 网络安全专家 NSE 5
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
开发固定资产管理系统,开发固定资产管理系统用什么语音
Golang并发编程——goroutine、channel、sync
第七章-类基础
聊聊电商系统中红包活动设计
守望先锋世界观架构 ——(一款好的游戏是怎么来的)
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
中国信通院《数据安全产品与服务图谱》,美创科技实现四大板块全覆盖
使用xml文件打印mybaties-log插件的方式
性能测试如何创造业务价值
为什么要做企业固定资产管理系统,企业如何加强固定资产管理
How to play when you travel to Bangkok for the first time? Please keep this money saving strategy
以太网PHY层芯片LAN8720A简介
[100 cases of JVM tuning practice] 01 - introduction of JVM and program counter
[error record] problems related to the installation of the shuttle environment (follow-up error handling after executing the shuttle doctor command)
MySQL advanced (Advanced) SQL statement
横向越权与纵向越权[通俗易懂]
NPOI导出Excel2007