当前位置:网站首页>LeetCode 871. 最低加油次数
LeetCode 871. 最低加油次数
2022-07-03 08:49:00 【Sasakihaise_】

【贪心+优先队列】把当前油量能到达的加油站全部加入到优先队列中,按照加油量进行排序,如果当前不能到达终点,就把队列中最大的油加上,然后继续把找加完油之后能到达的加油站加入队列,如果最后队列为空还没能达到终点就返回-1.
以样例3为:
target = 100, startFuel = 10, stations = [[10,60],[20,30],[30,30],[60,40]]
1.最初有10,能到达的只有[10, 60],把他加入队列。
2.由于当前只行驶到10,还不到100,因此需要中途加油,将队列中的60拿出来加油,此时可以行驶到70,于是把后面70之前的加油站都加入队列,并按照油量排序后为:[60, 40], [20, 30], [30, 30]
3.当前行驶了70,需要继续加油,取出队列中最大的40,加完后总里程为110可到达终点。
贪心的精髓在于,在当前油量能到达的加油站中尽可能选择油量高的加油站加油。
class Solution {
// 贪心 找能到达的加油站中加的最多的
public int minRefuelStops(int target, int startFuel, int[][] stations) {
int sum = startFuel;
PriorityQueue<int[]> queue = new PriorityQueue<int[]>((a, b) -> { return b[1] - a[1]; });
int i = 0, n = stations.length, ans = 0;
while (true) {
if (sum >= target) {
return ans;
}
for (; i < n; i++) {
if (stations[i][0] <= sum) {
queue.offer(stations[i]);
} else {
break;
}
}
if (queue.isEmpty()) {
return -1;
}
int[] top = queue.poll();
sum += top[1];
ans++;
}
}
}边栏推荐
- Annotations simplify configuration and loading at startup
- [public key cryptography] ECC elliptic cryptosystem (implementing ElGamal encryption method)
- Convert video to GIF
- [concurrent programming] synchronization container, concurrent container, blocking queue, double ended queue and work secret
- Unity learning notes
- How to deal with the core task delay caused by insufficient data warehouse resources
- Mortgage Calculator
- Tree DP acwing 285 A dance without a boss
- Alibaba canaladmin deployment and canal cluster Ha Construction
- Campus lost and found platform based on SSM, source code, database script, project import and operation video tutorial, Thesis Writing Tutorial
猜你喜欢
![[concurrent programming] explicit lock and AQS](/img/5f/a80751a68726f53d11133810f454a3.jpg)
[concurrent programming] explicit lock and AQS

单调栈-84. 柱状图中最大的矩形

Divide candy (circular queue)

Deeply understand the underlying data structure of MySQL index

UE4 source code reading_ Bone model and animation system_ Animation compression

数位统计DP AcWing 338. 计数问题

JS non Boolean operation - learning notes

Tree DP acwing 285 A dance without a boss

On the setting of global variable position in C language
![[set theory] order relation (total order relation | total order set | total order relation example | quasi order relation | quasi order relation theorem | bifurcation | quasi linear order relation | q](/img/76/6561a78b7f883a0e75a53e037153c3.jpg)
[set theory] order relation (total order relation | total order set | total order relation example | quasi order relation | quasi order relation theorem | bifurcation | quasi linear order relation | q
随机推荐
Try to reprint an article about CSDN reprint
How to place the parameters of the controller in the view after encountering the input textarea tag in the TP framework
Unity learning notes
如何应对数仓资源不足导致的核心任务延迟
Allocation exception Servlet
Find the combination number acwing 885 Find the combination number I
[rust notes] 08 enumeration and mode
Animation_ IK overview
<, < <,>, > > Introduction in shell
【Rust笔记】05-错误处理
[linear table] basic operation of bidirectional linked list specify node exchange
Dom4j traverses and updates XML
Annotations simplify configuration and loading at startup
TP5 multi condition sorting
[concurrent programming] collaboration between threads
Character pyramid
SQL statement error of common bug caused by Excel cell content that is not paid attention to for a long time
[rust notes] 05 error handling
[set theory] order relation (total order relation | total order set | total order relation example | quasi order relation | quasi order relation theorem | bifurcation | quasi linear order relation | q
Concurrent programming (V) detailed explanation of atomic and unsafe magic classes