当前位置:网站首页>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
边栏推荐
- Web2.0的巨头纷纷布局VC,Tiger DAO VC或成抵达Web3捷径
- 2022 software engineering final exam recall Edition
- MySQL advanced learning summary 8: overview of InnoDB data storage structure page, internal structure of page, row format
- Web2.0 giants have deployed VC, and tiger Dao VC may become a shortcut to Web3
- Tutorial (5.0) 10 Troubleshooting * fortiedr * Fortinet network security expert NSE 5
- 预处理和预处理宏
- Mysql高级篇学习总结8:InnoDB数据存储结构页的概述、页的内部结构、行格式
- End to end object detection with transformers (Detr) paper reading and understanding
- [0701] [论文阅读] Alleviating Data Imbalance Issue with Perturbed Input During Inference
- 注解开发方式下AutowiredAnnotationBeanPostProcessor的注册时机
猜你喜欢

According to the atlas of data security products and services issued by the China Academy of information technology, meichuang technology has achieved full coverage of four major sectors

消息队列消息丢失和消息重复发送的处理策略

Advanced performance test series "24. Execute SQL script through JDBC"

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

教程篇(5.0) 09. RESTful API * FortiEDR * Fortinet 网络安全专家 NSE 5

高级性能测试系列《24. 通过jdbc执行sql脚本》

高频面试题

In pytorch function__ call__ And forward functions

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

The difference between interceptor and filter
随机推荐
R language dplyr package Na_ The if function converts the control in the vector value into the missing value Na, and converts the specified content into the missing value Na according to the mapping r
Develop fixed asset management system, what voice is used to develop fixed asset management system
【ERP软件】ERP体系二次开发有哪些危险?
教程篇(5.0) 10. 故障排除 * FortiEDR * Fortinet 网络安全专家 NSE 5
Mysql高级篇学习总结7:Mysql数据结构-Hash索引、AVL树、B树、B+树的对比
机器学习笔记 - 时间序列预测研究:法国香槟的月销量
GMapping代码解析[通俗易懂]
Machine learning notes - time series prediction research: monthly sales of French champagne
A4988驱动步进电机「建议收藏」
虚拟机初始化脚本, 虚拟机相互免秘钥
Stm32g0 USB DFU upgrade verification error -2
Transformation of thinking consciousness is the key to the success or failure of digital transformation of construction enterprises
移动机器人路径规划:人工势场法[通俗易懂]
metric_ Logger urination
How to print mybats log plug-in using XML file
Golang concurrent programming goroutine, channel, sync
Markdown basic grammar
PHP非对称加密方法私钥及公钥加密解密的方法
#gStore-weekly | gStore源码解析(四):安全机制之黑白名单配置解析
横向越权与纵向越权[通俗易懂]