Leetcode 1094. Car Pooling
You are driving a vehicle that has capacity empty seats initially available for passengers.The vehicle only drives east (ie. it cannot turn around and drive west.)Given a list of trips, trip = contains information about the i-th trip: the number of passengers that must be picked up, and the locations to pick them up and drop them off.The locations are given as the number of kilometers due east from your vehicle's initial location.
Return true if and only if it is possible to pick up and drop off all passengers for all the given trips.
Example 1:
Input: trips = [,], capacity = 4
Output: false
Example 2:
Input: trips = [,], capacity = 5
Output: true
Example 3:
Input: trips = [,], capacity = 3
Output: true
Example 4:
Input: trips = [,,], capacity = 11
Output: true
Constraints:
trips.length <= 1000
trips.length == 3
1 <= trips <= 100
0 <= trips < trips <= 1000
1 <= capacity <= 100000
class Solution:
def carPooling(self, trips: List], capacity: int) -> bool:
if trips == None or len(trips) == 0 or len(trips) == 0:
return True
trips.sort(key = lambda x: x)
heap = []
for trip in trips:
heapq.heappush(heap, , trip, trip])
# print(heap)
while heap:
curt = heapq.heappop(heap)
curt_passenger = curt
curt_start = curt
curt_end = curt
next = None
if heap and heap < curt_end:
next = heapq.heappop(heap)
next_passenger = next
next_start = next
next_end = next
if next_end > curt_end:
heapq.heappush(heap, )
heapq.heappush(heap, )
heapq.heappush(heap, )
else:
heapq.heappush(heap, )
heapq.heappush(heap, )
heapq.heappush(heap, )
if next == None:
if curt_passenger > capacity:
return False
return True
页:
[1]