-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlc-56.py
34 lines (29 loc) · 938 Bytes
/
lc-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
24
25
26
27
28
29
30
31
32
33
34
import heapq
class Solution:
def overlap(self, a, b):
print( f'Checking overlap for : {a} and {b} ')
a_start = a[0]
a_end = a[1]
b_start = b[0]
b_end = b[1]
overlapping = False
if a_end > b_start:
overlapping = True
if not overlapping:
return None
else:
return [ a_start, max(a_end, b_end) ]
def merge(self, intervals):
heapq.heapify(intervals)
merged = [ heapq.heappop(intervals) ]
while( intervals ):
interval = heapq.heappop(intervals)
if self.overlap( merged[-1], interval):
merged[-1] = self.overlap( merged[-1], interval)
else:
merged.append( interval )
return merged
if __name__ == '__main__':
intervals = [[1,3],[2,6],[8,10],[15,18]]
sol = Solution()
print( sol.merge( intervals ) )