class Solution:
def insert(self, intervals: List[List[int]], newInterval: List[int]) -> List[List[int]]:
new_intervals = []
insertIndex = len(intervals)
for i in range(len(intervals)):
if intervals[i][0] > newInterval[0]:
insertIndex = i
break
intervals.insert(insertIndex, newInterval)
stack = [intervals[0]]
for i in range(1, len(intervals)):
prevInterval = stack.pop()
newInterval = intervals[i]
if prevInterval[1] >= newInterval[0]:
newInterval = [min(prevInterval[0], newInterval[0]), max(prevInterval[1], newInterval[1])]
else:
stack.append(prevInterval)
stack.append(newInterval)
return stack