forked from varunu28/LeetCode-Java-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCar Pooling.java
More file actions
22 lines (19 loc) · 704 Bytes
/
Car Pooling.java
File metadata and controls
22 lines (19 loc) · 704 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Solution {
public boolean carPooling(int[][] trips, int capacity) {
PriorityQueue<Integer> end = new PriorityQueue<>(Comparator.comparingInt(o -> trips[o][2]));
Arrays.sort(trips, Comparator.comparingInt(o -> o[1]));
end.add(0);
int currCapacity = trips[0][0];
for (int i = 1; i < trips.length; i++) {
while (!end.isEmpty() && trips[i][1] >= trips[end.peek()][2]) {
currCapacity -= trips[end.poll()][0];
}
if (currCapacity + trips[i][0] > capacity) {
return false;
}
currCapacity += trips[i][0];
end.add(i);
}
return true;
}
}