-
Notifications
You must be signed in to change notification settings - Fork 14
/
Solution871.java
35 lines (29 loc) · 1.02 KB
/
Solution871.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package leetcode.priorityqueue;
import java.util.Arrays;
import java.util.Comparator;
import java.util.PriorityQueue;
public class Solution871 {
public static void main(String[] args) {
// 2
System.out.println(new Solution871().minRefuelStops(100, 10, new int[][]{{10, 60}, {20, 30}, {30, 30}, {60, 40}}));
}
public int minRefuelStops(int target, int startFuel, int[][] stations) {
Arrays.sort(stations, Comparator.comparingInt(x -> x[0]));
// 保存油量
PriorityQueue<Integer> priorityQueue = new PriorityQueue<>((x, y) -> y - x);
int index = 0;
int res = 0;
while (startFuel < target) {
while (index < stations.length && startFuel >= stations[index][0]) {
priorityQueue.offer(stations[index++][1]);
}
if (priorityQueue.isEmpty()) {
return -1;
} else {
startFuel += priorityQueue.poll();
res++;
}
}
return res;
}
}