Skip to content

Commit 253e1a4

Browse files
Time: 3 ms (63.58%) | Memory: 18.2 MB (76.83%) - LeetSync
1 parent 20765da commit 253e1a4

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

1184-car-pooling/car-pooling.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import heapq
2+
3+
class Solution:
4+
def carPooling(self, trips: List[List[int]], capacity: int) -> bool:
5+
trips = sorted(trips, key=lambda x: x[1])
6+
7+
in_car = []
8+
9+
for num, start, end in trips:
10+
capacity -= num
11+
12+
heapq.heappush(in_car, (end, num))
13+
14+
while in_car and in_car[0][0] <= start:
15+
capacity += in_car[0][1]
16+
heapq.heappop(in_car)
17+
18+
if capacity < 0:
19+
return False
20+
21+
return True

0 commit comments

Comments
 (0)