-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathmerge.py
24 lines (20 loc) · 914 Bytes
/
merge.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
from typing import List
class Solution:
def merge(self, intervals: List[List[int]]) -> List[List[int]]:
intervals.sort(key=lambda x: x[0])
merged = []
for interval in intervals:
# if the list of merged intervals is empty or if the current
# interval does not overlap with the previous, simply append it.
if not merged or merged[-1][1] < interval[0]:
merged.append(interval)
else:
# otherwise, there is overlap, so we merge the current and previous
# intervals.
merged[-1][1] = max(merged[-1][1], interval[1])
return merged
# Checking in PyCharm/terminal:
if __name__ == '__main__':
Instant = Solution()
Solve = Instant.merge([[1,3],[2,6],[8,10],[15,18]]) # [[1,3],[2,6],[8,10],[15,18]] -> [[1, 6], [8, 10], [15, 18]] | [[1,4],[4,5]] -> [[1,5]]
print(Solve)