from heapq import heappush, heappop
class Solution:
def carPooling(self, trips: List[List[int]], capacity: int) -> bool:
if not trips:
return False
pq = []
for pp, start, end in trips:
heappush(pq,(start,pp))
heappush(pq,(end,-pp))
while pq:
capacity -= heappop(pq)[1]
if capacity < 0:
return False
return True
optimization
from heapq import heappush, heappop
class Solution:
def carPooling(self, trips: List[List[int]], capacity: int) -> bool:
if not trips:
return False
stops = [0]* 1001
for pp, s, e in trips:
stops[s] -= pp
stops[e] += pp
for stop in stops:
capacity += stop
if capacity < 0:
return False
return True