Skip to content

Instantly share code, notes, and snippets.

@wkdalsgh192
Created January 8, 2021 13:40
Show Gist options
  • Save wkdalsgh192/00ece1be0758e81f01165f6927ff407f to your computer and use it in GitHub Desktop.
Save wkdalsgh192/00ece1be0758e81f01165f6927ff407f to your computer and use it in GitHub Desktop.
/*
https://leetcode.com/problems/car-pooling/
*/
class Solution {
public boolean carPooling(int[][] trips, int capacity) {
int[] pick = new int[1001], drop = new int[1001];
for(int i=0;i<trips.length;i++) {
pick[trips[i][1]]+=trips[i][0];
drop[trips[i][2]]+=trips[i][0];
}
int sp = 1;
while (sp < 1001) {
if (drop[sp] > 0) capacity += drop[sp];
if (pick[sp] > 0) {
capacity -= pick[sp];
if (capacity < 0) return false;
}
sp++;
}
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment