-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathLeetCode-1094-Car-Pooling.java
More file actions
59 lines (50 loc) · 1.63 KB
/
LeetCode-1094-Car-Pooling.java
File metadata and controls
59 lines (50 loc) · 1.63 KB
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
class Solution {
// 1. One Way Scan
/*
https://leetcode.com/problems/car-pooling/discuss/317611/C%2B%2BJava-O(n)-Thousand-and-One-Stops
*/
// public boolean carPooling(int[][] trips, int capacity) {
// int[] stops = new int[10001];
// for (int[] t : trips) {
// stops[t[1]] += t[0];
// stops[t[2]] -= t[0];
// }
// for (int i = 0; i < 10001; i++) {
// capacity -= stops[i];
// if (capacity < 0) return false;
// }
// return true;
// }
/*
https://leetcode.com/problems/car-pooling/discuss/317887/Java-Clean-code-use-sort-and-PriorityQueue.
Example:
[[2,2,6],[2,4,7],[8,6,7]]
11
The PQ:
2 2
4 2
6 8
6 -2
7 -8
7 -2
If one trip starts at 6, another trip ends at 6, we should let those ends be first (let passenger went down first), then let passenger of next trip went up.
So the following PQ definition doesn't work:
PriorityQueue<int[]> pq = new PriorityQueue<>(Comparator.comparing(t -> t[0]));
*/
public boolean carPooling(int[][] trips, int capacity) {
PriorityQueue<int[]> pq = new PriorityQueue<>((a, b) -> a[0] == b[0] ? a[1] - b[1] : a[0] - b[0]);
for (int[] t : trips) {
pq.offer(new int[] {t[1], t[0]});
pq.offer(new int[] {t[2], -t[0]});
}
// printPQ(pq);
while (!pq.isEmpty()) {
int[] t = pq.poll();
capacity -= t[1];
if (capacity < 0) {
return false;
}
}
return true;
}
}