Created
January 8, 2021 13:40
-
-
Save wkdalsgh192/00ece1be0758e81f01165f6927ff407f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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