-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcarPooling.py
More file actions
20 lines (18 loc) · 774 Bytes
/
carPooling.py
File metadata and controls
20 lines (18 loc) · 774 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# problem: https://leetcode.com/problems/car-pooling/
# Runtime: 90 ms, faster than 25.24% of Python3 online submissions for Car Pooling.
# Memory Usage: 15.4 MB, less than 18.85% of Python3 online submissions for Car Pooling.
from typing import List
class Solution:
def carPooling(self, trips: List[List[int]], capacity: int) -> bool:
from sortedcontainers import SortedList
passengers = SortedList()
trips.sort(key=lambda x: x[1])
for num, from_, to in trips:
while passengers and passengers[0][0] <= from_:
_, b = passengers.pop(0)
capacity += b
capacity -= num
passengers.add((to, num))
if capacity < 0:
return False
return True