-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path57_Insert_Interval.py
45 lines (40 loc) · 1.42 KB
/
57_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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# O(NlogN)
class Solution:
def insert(self, intervals: List[List[int]], newInterval: List[int]) -> List[List[int]]:
intervals.append(newInterval)
return self.merge(intervals)
def merge(self, intervals):
if len(intervals)<2:
return intervals
intervals.sort(key=lambda x: x[0])
res=[]
cur=[]
for i in range(len(intervals)):
if i == 0:
cur = intervals[i]
i+=1
continue
if cur[1] >= intervals[i][0]:
cur[1] = max(cur[1],intervals[i][1])
else:
res.append(cur)
cur = intervals[i]
res.append(cur)
return res
# O(N)
class Solution_2:
def insert(self, intervals: List[List[int]], newInterval: List[int]) -> List[List[int]]:
result = []
# keep updating newInterval until it does not overlap with any interlval in the list
for i,cur in enumerate(intervals):
if cur[1]<newInterval[0]:
result.append(cur)
elif cur[0] > newInterval[1]:
result.append(newInterval)
result.extend(intervals[i:])
return result
else:
newInterval[0] = min(cur[0],newInterval[0])
newInterval[1] = max(cur[1],newInterval[1])
result.append(newInterval)
return result