#57
Insert Interval
medium· Intervalsruns: 0You are given an array of non-overlapping intervals where intervals[i] = [start_i, end_i] sorted in ascending order by start. You are also given a new interval [start, end] to insert. Return intervals after the insertion, with overlapping intervals merged.
sign in to paste and practice your own solution
wpm 0acc 100%time 0:000 / 665
class Solution:
def insert(
self,
intervals: list[list[int]],
newInterval: list[int],
) -> list[list[int]]:
result = []
i = 0
n = len(intervals)
while i < n and intervals[i][1] < newInterval[0]:
result.append(intervals[i])
i += 1
while i < n and intervals[i][0] <= newInterval[1]:
newInterval[0] = min(newInterval[0], intervals[i][0])
newInterval[1] = max(newInterval[1], intervals[i][1])
i += 1
result.append(newInterval)
while i < n:
result.append(intervals[i])
i += 1
return result
click the box to focus · tab inserts 4 spaces · backspace to correct · esc to pause
desktop only
codedrill is a typing game and needs a real keyboard. open this on a laptop or desktop to practice.
you can still browse problems and sections from your phone.