-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoverlapping_intervals.py
33 lines (24 loc) · 1 KB
/
overlapping_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
"""Given a list of possibly overlapping intervals,
return a new list of intervals where all overlapping intervals have been merged."""
class Intervals:
"""class of intervals having start and end point."""
def __init__(self, s=0, e=0):
"""Initializer method for Intervals."""
self.start = s
self.end = e
def __repr__(self):
"""Function to return a list with start and end points."""
return "[{}, {}]".format(self.start, self.end)
class Solution:
"""class to find a overlapping intervals."""
def merge(self, intervals):
intervals = sorted(intervals, key=lambda x: x.start)
out = []
for i in intervals:
if out and i.start <= out[-1].end:
out[-1].end = max(out[-1].end, i.end)
else:
out.append(i)
return out
interval = [Intervals(1, 2), Intervals(6, 11), Intervals(0, 5), Intervals(7, 9), Intervals(1, 2)]
print(Solution().merge(interval))