-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path57_Insert_Interval.py
More file actions
29 lines (25 loc) · 1.03 KB
/
Copy path57_Insert_Interval.py
File metadata and controls
29 lines (25 loc) · 1.03 KB
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
class Solution:
def insert(self, intervals: List[List[int]], newInterval: List[int]) -> List[List[int]]:
res = []
for current in intervals:
if current[0] < newInterval[0]:
if current[1] >= newInterval[0]:
# merge two intervals
newInterval = self.merge(current, newInterval)
else:
res.append(current)
elif current[0] > newInterval[0]:
if current[0] <= newInterval[1]:
# merge
newInterval = self.merge(current, newInterval)
else:
res.append(current)
else:
# merge
newInterval = self.merge(current, newInterval)
res.append(newInterval)
res.sort(key=lambda x: x[0])
return res
def merge(self, interval1: [int], interval2: [int]) -> [int]:
newInterval = [min(interval1[0], interval2[0]), max(interval1[1], interval2[1])]
return newInterval