Skip to content

Latest commit

 

History

History
47 lines (33 loc) · 1.18 KB

_57. Insert Interval.md

File metadata and controls

47 lines (33 loc) · 1.18 KB

All prompts are owned by LeetCode. To view the prompt, click the title link above.

Back to top


First completed : June 19, 2024

Last updated : June 19, 2024


Related Topics : Array

Acceptance Rate : 42.33 %


Solutions

Python

class Solution:
    def insert(self, intervals: List[List[int]], newInterval: List[int]) -> List[List[int]]:
        if not intervals :
            return [newInterval]
        
        output = []
        intervals.append(newInterval)
        intervals.sort(key=lambda x : x[0], reverse=True)
        output.append(intervals.pop())

        while intervals :
            start, end = intervals.pop()

            if output[-1][1] < start :
                output.append([start, end])
            elif output[-1][1] < end :
                output[-1][1] = end
        
        return output