forked from ngiengkianyew/daily-coding-problem
-
Notifications
You must be signed in to change notification settings - Fork 2
/
problem_077.py
33 lines (29 loc) · 1.07 KB
/
problem_077.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
def merge_overlaps(intervals):
interval_starts, interval_ends = set(), set()
for start, end in intervals:
interval_starts.add(start)
interval_ends.add(end)
min_start = min(interval_starts)
max_end = max(interval_ends)
current_active = 0
instant_statuses = list([current_active])
merged = list()
for i in range(min_start, max_end + 1):
if i in interval_ends:
current_active -= 1
if i in interval_starts:
current_active += 1
instant_statuses.append(current_active)
start, end = None, None
for i in range(len(instant_statuses)):
if instant_statuses[i] and not instant_statuses[i-1]:
start = i
if not instant_statuses[i] and instant_statuses[i-1]:
end = i
merged.append((start, end))
start, end = None, None
return merged
assert merge_overlaps([(1, 3), (5, 8), (4, 10), (20, 25)]) == [
(1, 3), (4, 10), (20, 25)]
assert merge_overlaps([(1, 3), (5, 8), (4, 10), (20, 25), (6, 12)]) == [
(1, 3), (4, 12), (20, 25)]