-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1094_carPooling.py
23 lines (18 loc) · 980 Bytes
/
1094_carPooling.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# 1094. Car Pooling
class Solution:
def carPooling(self, trips: List[List[int]], capacity: int) -> bool:
# Because from and to is between 0 and 1000. Idea is to store counts in an array of size 1001.
lengthOfTrip = [ 0 for _ in range(1001)]
# Update passenger change for each trip
for trip, i, j in trips:
lengthOfTrip[i] += trip # Increment when passenger is on board
lengthOfTrip[j] -= trip # decrement when they depart
print(lengthOfTrip[:8])
carLoad = 0
# Count total passenger for each bus top
# we have the count array, we perform a line sweep from 0 to 1000 and track its total
for i in range( len(lengthOfTrip) ):
carLoad += lengthOfTrip[i]
if carLoad > capacity: # Reject when the car is overloaded somewhere
return False
return True # Accept only if all trip is safe