当前位置:网站首页>LeetCode 871. Minimum refueling times
LeetCode 871. Minimum refueling times
2022-07-03 09:02:00 【Sasakihaise_】
【 greedy + Priority queue 】 Add all the gas stations that can reach the current fuel volume to the priority queue , Sort according to the refueling volume , If you can't reach the destination at present , Just add the largest oil in the queue , Then continue to join the queue of gas stations that can arrive after filling up , If the last queue is empty, it will return before reaching the end -1.
Take an example 3 by :
target = 100, startFuel = 10, stations = [[10,60],[20,30],[30,30],[60,40]]
1. At first there was 10, Only [10, 60], Add him to the queue .
2. At present, it only drives to 10, Not yet 100, Therefore, we need to refuel halfway , Put... In the queue 60 Take it out and refuel , You can drive to 70, So put the back 70 Previous gas stations have joined the queue , And according to the oil quantity, it is :[60, 40], [20, 30], [30, 30]
3. Currently driving 70, Need to continue to refuel , Get the largest in the queue 40, The total mileage after adding is 110 You can reach the end .
The essence of greed lies in , Choose the gas station with high fuel volume as far as possible among the gas stations that can reach the current fuel volume .
class Solution {
// greedy Find the gas station that can be reached with the most
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++;
}
}
}
边栏推荐
- First Servlet
- Gaussian elimination acwing 883 Gauss elimination for solving linear equations
- LeetCode 532. 数组中的 k-diff 数对
- 22-06-27 Xian redis (01) commands for installing five common data types: redis and redis
- Apache startup failed phpstudy Apache startup failed
- 拯救剧荒,程序员最爱看的高分美剧TOP10
- PHP mnemonic code full text 400 words to extract the first letter of each Chinese character
- LeetCode 515. 在每个树行中找最大值
- 教育信息化步入2.0,看看JNPF如何帮助教师减负,提高效率?
- [concurrent programming] collaboration between threads
猜你喜欢
Drawing maze EasyX library with recursive backtracking method
20220630 learning clock in
推荐一个 yyds 的低代码开源项目
Concurrent programming (V) detailed explanation of atomic and unsafe magic classes
PHP uses foreach to get a value in a two-dimensional associative array (with instances)
Format - C language project sub file
Low code momentum, this information management system development artifact, you deserve it!
Parameters of convolutional neural network
How to use Jupiter notebook
Phpstudy 80 port occupied W10 system
随机推荐
Log4j2 vulnerability recurrence and analysis
Baidu editor ueeditor changes style
教育信息化步入2.0,看看JNPF如何帮助教师减负,提高效率?
Deep parsing JVM memory model
记忆化搜索 AcWing 901. 滑雪
Method of intercepting string in shell
Concurrent programming (VI) ABA problems and solutions under CAS
Facial expression recognition based on pytorch convolution -- graduation project
低代码起势,这款信息管理系统开发神器,你值得拥有!
AcWing 785. Quick sort (template)
The method of replacing the newline character '\n' of a file with a space in the shell
Dom4j traverses and updates XML
Introduction to the usage of getopts in shell
[rust notes] 13 iterator (Part 1)
Divide candy (circular queue)
Try to reprint an article about CSDN reprint
LeetCode 508. The most frequent subtree elements and
Methods of using arrays as function parameters in shell
22-06-28 西安 redis(02) 持久化机制、入门使用、事务控制、主从复制机制
TP5 order multi condition sort