1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| class Solution { public boolean carPooling(int[][] trips, int capacity) { Arrays.sort(trips, (arr1, arr2) -> arr1[1] - arr2[1]); PriorityQueue<int[]> priorityQueue = new PriorityQueue<>((arr1, arr2) -> arr1[2] - arr2[2]); int peopleCount = 0; for (int[] trip : trips) { while (!priorityQueue.isEmpty() && trip[1] >= priorityQueue.peek()[2]) { peopleCount -= priorityQueue.poll()[0]; } priorityQueue.offer(trip); peopleCount += trip[0]; if (peopleCount > capacity) { return false; } } return true; } }
|