본문 바로가기

IT/코딩테스트

[Leetcode] 57. Insert Interval

728x90

https://leetcode.com/problems/insert-interval/

class Solution:
    def insert(self, intervals: List[List[int]], newInterval: List[int]) -> List[List[int]]:
        new = []

        for i in range(len(intervals)):
            if newInterval[1]<intervals[i][0]:
                new.append(newInterval)
                return new+intervals[i:]
            elif newInterval[0]>intervals[i][1]:
                new.append(intervals[i])
            else:
                newInterval=[min(newInterval[0],intervals[i][0]),max(intervals[i][1],newInterval[1])]
        new.append(newInterval)

        return new
728x90