Intervals - Car Pooling
The following code attempts to implement the optimal car pooling solution. Which line contains a subtle bug that can cause incorrect capacity checks?
def carPooling(trips, capacity):
max_location = 0
for _, start, end in trips:
max_location = max(max_location, end)
diff = [0] * (max_location + 1)
for num, start, end in trips:
diff[start] += num
diff[end] += num # Bug here
current_passengers = 0
for i in range(max_location + 1):
current_passengers += diff[i]
if current_passengers > capacity:
return false
return true
