class Solution: def insert( self, intervals: List[List[int]], newInterval: List[int] ) -> List[List[int]]: res = [] for i in range(len(intervals)): if newInterval[1] < intervals[i][0]: res.append(newInterval) return res + intervals[i:] elif newInterval[0] > intervals[i][1]: res.append(intervals[i]) else: newInterval = [ min(newInterval[0], intervals[i][0]), max(newInterval[1], intervals[i][1]), ] res.append(newInterval) return res
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter