-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path56.py
23 lines (22 loc) · 898 Bytes
/
56.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# Merge Intervals
class Solution:
def merge(self, intervals: List[List[int]]) -> List[List[int]]:
if len(intervals) < 2:
return intervals
ret_val = []
intervals.sort(key=lambda x: x[0])
while len(intervals) > 0:
current_interval = intervals.pop(0)
i = 0
while i < len(intervals):
next_interval = intervals[i]
if next_interval[0] <= current_interval[1]:
next_interval = intervals.pop(i)
if current_interval[1] < next_interval[1]:
current_interval = [current_interval[0], next_interval[1]]
else:
current_interval = [current_interval[0], current_interval[1]]
else:
i += 1
ret_val.append(current_interval)
return ret_val