-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinsert_interval.py
31 lines (29 loc) · 1.07 KB
/
insert_interval.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
from typing import List
class InsertInterval:
@staticmethod
def insert(intervals: List[List[int]], newInterval: List[int]) -> List[List[int]]:
# List to store the final output
result = []
if intervals is None or len(intervals) == 0:
result.append(newInterval)
return result
# Total number of intervals
n = len(intervals)
# Add all intervals that are smaller than newInterval
index = 0
while index < n and intervals[index][1] < newInterval[0]:
result.append(intervals[index])
index += 1
# Add new interval and merge, if required
while index < n and intervals[index][0] <= newInterval[1]:
newInterval = [
min(intervals[index][0], newInterval[0]),
max(intervals[index][1], newInterval[1])
]
index += 1
result.append(newInterval)
# Add remaining elements to the list
while index < n:
result.append(intervals[index])
index += 1
return result