Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- '''
- 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.
- '''
- class Solution:
- def carPooling(self, trips: List[List[int]], cap: int) -> bool:
- t=[0]*1001
- for p,s,e in trips:
- t[s]+=p
- t[e]-=p
- cur=0
- for i in range(1001):
- cur=cur+t[i]
- if cur>cap:
- return False
- return True
- #standard method
- class Solution:
- def carPooling(self, a: List[List[int]], cap: int) -> bool:
- start,end=[],[]
- n=len(a)
- for p,s,e in a:
- start.append((s,p))
- end.append((e,p))
- start.sort()
- end.sort()
- i,j=0,0
- cur=0
- while i<n and j<n:
- if start[i][0]<end[j][0]:
- if cur+start[i][1]>cap:
- return False
- else:
- cur=cur+start[i][1]
- i+=1
- else:
- cur=cur-end[j][1]
- j+=1
- return True
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement