-
Notifications
You must be signed in to change notification settings - Fork 0
/
merge_intervals.py
36 lines (34 loc) · 1.86 KB
/
merge_intervals.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
class Solution:
def merge(self, intervals: List[List[int]]) -> List[List[int]]:
final = []
for interval in sorted(intervals, key=lambda i: i[0]):
if final and final[-1][1] >= interval[0]:
final[-1] = [final[-1][0], max(final[-1][1], interval[1])]
else:
final.append(interval)
return final
# final = []
# while intervals:
# curr_interval = intervals.pop()
# i = 0
# while i < len(intervals):
# if ((intervals[i][0] >= curr_interval[0] and intervals[i][0] <= curr_interval[1]) or #left inside
# (intervals[i][1] <= curr_interval[1] and intervals[i][1] >= curr_interval[0]) or # right inside
# (intervals[i][0] <= curr_interval[0] and intervals[i][1] >= curr_interval[1])): # entirely larger
# #entirely smaller is handeld by left, right inside logic
# curr_interval = [min(intervals[i][0], curr_interval[0]), max(intervals[i][1], curr_interval[1])]
# intervals.pop(i)
# else:
# i+= 1
# j = 0
# while j < len(final):
# if ((final[j][0] >= curr_interval[0] and final[j][0] <= curr_interval[1]) or #left inside
# (final[j][1] <= curr_interval[1] and final[j][1] >= curr_interval[0]) or # right inside
# (final[j][0] <= curr_interval[0] and final[j][1] >= curr_interval[1])): # entirely larger
# #entirely smaller is handeld by left, right inside logic
# curr_interval = [min(final[j][0], curr_interval[0]), max(final[j][1], curr_interval[1])]
# final.pop(j)
# else:
# j += 1
# final.append(curr_interval)
# return final