class Solution:
def carPooling(self, trips: List[List[int]], capacity: int) -> bool:
trips.sort(key=lambda x: x[1])
for i in range(len(trips)):
curPass = trips[i][0]
for j in range(i):
if trips[j][2] > trips[i][1]:
curPass += trips[j][0]
if curPass > capacity:
return False
return Trueclass Solution:
def carPooling(self, trips: List[List[int]], capacity: int) -> bool:
trips.sort(key=lambda t: t[1])
minHeap = [] # pair of [end, numPassengers]
curPass = 0
for numPass, start, end in trips:
while minHeap and minHeap[0][0] <= start:
curPass -= heapq.heappop(minHeap)[1]
curPass += numPass
if curPass > capacity:
return False
heapq.heappush(minHeap, [end, numPass])
return Trueclass Solution:
def carPooling(self, trips: List[List[int]], capacity: int) -> bool:
points = []
for passengers, start, end in trips:
points.append([start, passengers])
points.append([end, -passengers])
points.sort()
curPass = 0
for point, passengers in points:
curPass += passengers
if curPass > capacity:
return False
return Trueclass Solution:
def carPooling(self, trips: List[List[int]], capacity: int) -> bool:
L, R = float("inf"), float("-inf")
for _, start, end in trips:
L = min(L, start)
R = max(R, end)
N = R - L + 1
passChange = [0] * (N + 1)
for passengers, start, end in trips:
passChange[start - L] += passengers
passChange[end - L] -= passengers
curPass = 0
for change in passChange:
curPass += change
if curPass > capacity:
return False
return TrueWhere is the size of the array and is the difference between the rightmost location and the leftmost location.