Description
There is a car with capacity empty seats. The vehicle only drives east (i.e., it cannot turn around and drive west).
You are given the integer capacity and an array trips where trips[i] = [numPassengersi, fromi, toi] indicates that the ith trip has numPassengersi passengers and the locations to pick them up and drop them off are fromi and toi respectively. The locations are given as the number of kilometers due east from the car's initial location.
Return true if it is possible to pick up and drop off all passengers for all the given trips, or false otherwise.
Β
Example 1:
Input: trips = [[2,1,5],[3,3,7]], capacity = 4 Output: false
Example 2:
Input: trips = [[2,1,5],[3,3,7]], capacity = 5 Output: true
Β
Constraints:
1 <= trips.length <= 1000trips[i].length == 31 <= numPassengersi <= 1000 <= fromi < toi <= 10001 <= capacity <= 105
Solution
Python3
class Solution:
def carPooling(self, trips: List[List[int]], capacity: int) -> bool:
events = []
for count, a, b in trips:
events.append((a, 1, count))
events.append((b, 0, count))
events.sort()
curr = 0
for _, t, cnt in events:
if t == 0:
curr -= cnt
else:
curr += cnt
if curr > capacity:
return False
return True