import heapq
class Solution:
def carPooling(self, trips: List[List[int]], capacity: int) -> bool:
#先对trips按照上车地点和下车地点进行升序排序
trips = sorted(trips,key=lambda x:(x[1],x[2]))
heap = [[trips[0][2],trips[0][0]]]
capacity -= trips[0][0]
for i in trips[1:]:
#如果当前乘客的上车地点大于堆中一些乘客的下车地点,则将其弹出来并结算容量
while heap and heap[0][0] <= i[1]:
capacity += heap[0][1]
heapq.heappop(heap)
if capacity >= i[0]: #判断此时的容量是否满足上车的人数
capacity -= i[0]
heapq.heappush(heap,[i[2],i[0]])
else:
return False
return True
leetcode 1094. 拼车 python
最新推荐文章于 2022-09-09 16:15:00 发布