Skip to content

Commit ca600d9

Browse files
authored
Update car-pooling.py
1 parent 87b9c23 commit ca600d9

File tree

1 file changed

+3
-10
lines changed

1 file changed

+3
-10
lines changed

Python/car-pooling.py

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,16 @@
11
# Time: O(nlogn)
22
# Space: O(n)
33

4-
import collections
5-
6-
74
class Solution(object):
85
def carPooling(self, trips, capacity):
96
"""
107
:type trips: List[List[int]]
118
:type capacity: int
129
:rtype: bool
1310
"""
14-
lookup = collections.defaultdict(int)
15-
for num, start, end in trips:
16-
lookup[start] += num
17-
lookup[end] -= num
18-
intervals = [(location, num) for location, num in lookup.iteritems()]
19-
intervals.sort()
20-
for location, num in intervals:
11+
line = [x for num, start, end in trips for x in [[start, num], [end, -num]]]
12+
line.sort()
13+
for _, num in line:
2114
capacity -= num
2215
if capacity < 0:
2316
return False

0 commit comments

Comments
 (0)