LeetCode 1094 : Car Pooling
Solution to Car Pooling Problem.
class Solution {
public boolean carPooling(int[][] trips, int capacity) {
int[][] tripsSortedBasedOnEndingTime = trips.clone();
Arrays.sort(trips, (a,b) -> a[1] - b[1]);
Arrays.sort(tripsSortedBasedOnEndingTime, (a,b) -> a[2] - b[2]);
int j = 0;
for(int[] i : trips) {
while (tripsSortedBasedOnEndingTime[j][2] <= i[1]) {
capacity += tripsSortedBasedOnEndingTime[j][0];
j++;
}
if (capacity < i[0]) {
return false;
}
capacity -= i[0];
}
return true;
}
}