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 arraytrips
wheretrips[i] = [numPassengersi, fromi, toi]
indicates that theith
trip hasnumPassengersi
passengers and the locations to pick them up and drop them off arefromi
andtoi
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, orfalse
otherwise.
class Solution:
def carPooling(self, trips, capacity):
i = 0
people = 0
max_stop = trips[0][2]
trip_run = True
# 确定最大站数
for each in trips:
max_stop = max(max_stop, each[2])
while trip_run:
# 上下车
for each in trips:
if each[1] == i:
people += each[0]
elif each[2] == i:
people -= each[0]
# 超载,break
if people > capacity:
trip_run = False
break
# 到站,break
if i == max_stop:
break
else:
i += 1
return trip_run