class Solution:
def insert(self, intervals: List[List[int]], newInterval: List[int]) -> List[List[int]]:
intervals.sort(key = lambda data : data[0])
length = len(intervals)
if length <= 0:
return [newInterval]
if length == 1:
if intervals[0][1] >= newInterval[0] and intervals[0][1] <= newInterval[1]:
temp = []
left = min(intervals[0][0], newInterval[0])
temp.append(left)
right = max(intervals[0][1], newInterval[1])
temp.append(right)
return [temp]
elif intervals[0][1] < newInterval[0]:
temp_1 = []
left = intervals[0][0]
temp_1.append(left)
right = intervals[0][1]
temp_1.append(right)
temp_2 = []
left = newInterval[0]
temp_2.append(left)
right = newInterval[1]
temp_2.append(right)
result = []
result.append(temp_1)
result.append(temp_2)
return result
elif intervals[0][0] > newInterval[1]:
temp_3 = []
left = newInterval[0]
temp_3.append(left)
right = newInterval[1]
temp_3.append(right)
temp_4 = []
left = intervals[0][0]
temp_4.append(left)
right = intervals[0][1]
temp_4.append(right)
result_1 = []
result_1.append(temp_3)
result_1.append(temp_4)
return result_1
elif intervals[0][0] >= newInterval[0] and intervals[0][1] > newInterval[1]:
temp_5 = []
left = min(intervals[0][0], newInterval[0])
temp_5.append(left)
right = max(intervals[0][1], newInterval[1])
temp_5.append(right)
return [temp_5]
index = 1
while index < len(intervals):
if newInterval[0] >= intervals[index - 1][0] and newInterval[0] < intervals[index - 1][1]:
if newInterval[1] >= intervals[index - 1][1] and newInterval[1] < intervals[index][0]:
intervals[index - 1][0] = intervals[index - 1][0]
intervals[index - 1][1] = newInterval[1]
elif newInterval[1] <= intervals[index - 1][1]:
intervals[index - 1][0] = intervals[index - 1][0]
intervals[index - 1][1] = intervals[index - 1][1]
elif intervals[index][0] <= newInterval[1] and newInterval[1] < intervals[index][1]:
intervals[index - 1][0] = intervals[index - 1][0]
intervals[index - 1][1] = intervals[index][1]
elif newInterval[1] >= intervals[index][1]:
del intervals[index]
intervals[index - 1][0] = intervals[index - 1][0]
intervals[index - 1][1] = newInterval[1]
#del intervals[index]
elif newInterval[0] > intervals[index - 1][0] and newInterval[0] <= intervals[index - 1][1]:
if newInterval[1] >= intervals[index - 1][1] and newInterval[1] < intervals[index][0]:
intervals[index - 1][0] = intervals[index - 1][0]
intervals[index - 1][1] = newInterval[1]
elif newInterval[1] <= intervals[index - 1][1]:
intervals[index - 1][0] = intervals[index - 1][0]
intervals[index - 1][1] = intervals[index - 1][1]
elif intervals[index][0] <= newInterval[1] and newInterval[1] < intervals[index][1]:
intervals[index - 1][0] = intervals[index - 1][0]
intervals[index - 1][1] = intervals[index][1]
elif newInterval[1] >= intervals[index][1]:
del intervals[index]
intervals[index - 1][0] = intervals[index - 1][0]
intervals[index - 1][1] = newInterval[1]
elif newInterval[1] < intervals[index - 1][0]:
intervals.insert(index - 1, newInterval)
elif newInterval[0] > intervals[index - 1][1]:
if newInterval[1] < intervals[index][0]:
intervals.insert(index, newInterval)
elif newInterval[1] >= intervals[index][0] and newInterval[1] < intervals[index][1]:
#del intervals[index]
intervals[index][0] = intervals[index][0]
intervals[index][1] = newInterval[1]
elif newInterval[1] >= intervals[index][0] and newInterval[1] >= intervals[index][1]:
#del intervals[index]
intervals[index][0] = intervals[index][0]
intervals[index][1] = newInterval[1]
elif newInterval[0] < intervals[index - 1][1]:
if newInterval[1] <= intervals[index - 1][1]:
intervals[index - 1][0] = newInterval[0]
intervals[index - 1][1] = intervals[index - 1][1]
elif newInterval[1] > intervals[index - 1][1] and newInterval[1] < intervals[index][0]:
intervals[index - 1][0] = newInterval[0]
intervals[index - 1][1] = intervals[index - 1][1]
elif newInterval[1] > intervals[index - 1][1] and newInterval[1] >= intervals[index][0]:
if newInterval[1] < intervals[index][1]:
intervals[index - 1][0] = newInterval[0]
intervals[index - 1][1] = intervals[index][1]
else:
intervals[index - 1][0] = newInterval[0]
intervals[index - 1][1] = newInterval[1]
index += 1
index_1 = 1
while index_1 < len(intervals):
if intervals[index_1][0] <= intervals[index_1 - 1][1]:
intervals[index_1 - 1][1] = max(intervals[index_1 - 1][1], intervals[index_1][1])
del intervals[index_1]
else:
index_1 += 1
return intervals