-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcarPooling.py
42 lines (32 loc) · 1.08 KB
/
carPooling.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
class Solution:
def carPooling(self, trips: List[List[int]], capacity: int) -> bool:
hm = collections.Counter()
for c, s, e in trips:
hm[s] += -c
hm[e] += c
for i in sorted(hm):
capacity += hm[i]
if capacity < 0:
return False
return True
"""
time: O(nlogn)
space: O(n)
"""
class Solution:
def carPooling(self, trips: List[List[int]], capacity: int) -> bool:
heap = []
for c, s, e in trips:
heapq.heappush(heap, (e, -c))
heapq.heappush(heap, (s, c))
while heap:
capacity -= heapq.heappop(heap)[1]
if capacity < 0:
return False
return True
def carPooling(self, trips: List[List[int]], capacity: int) -> bool:
stops = [0] * 1001
for c, s, e in trips:
stops[s] += c
stops[e] -= c
return all(x <= capacity for x in itertools.accumulate(stops))