- Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy path_1094.java
28 lines (26 loc) · 952 Bytes
/
_1094.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
packagecom.fishercoder.solutions.secondthousand;
importjava.util.Arrays;
importjava.util.PriorityQueue;
publicclass_1094 {
publicstaticclassSolution1 {
publicbooleancarPooling(int[][] trips, intcapacity) {
Arrays.sort(trips, (a, b) -> a[1] - b[1]);
PriorityQueue<int[]> heap = newPriorityQueue<>((a, b) -> a[1] - b[1]);
for (int[] trip : trips) {
intstartTime = trip[1];
intendTime = trip[2];
while (!heap.isEmpty() && heap.peek()[1] <= startTime) {
int[] curr = heap.poll();
capacity += curr[0];
}
intpeopleCnt = trip[0];
capacity -= peopleCnt;
if (capacity < 0) {
returnfalse;
}
heap.offer(newint[] {peopleCnt, endTime});
}
returntrue;
}
}
}