https://leetcode.com/problems/merge-intervals/description/

 

LeetCode - The World's Leading Online Programming Learning Platform

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

 

정렬하고 스캔하면서 작업.

지금까지 추가한 인터벌의 end값 중에 제일 큰 end값을 유지하면서 갱신해나간다.

 

class Solution:
    def merge(self, intervals: List[List[int]]) -> List[List[int]]:
        intervals.sort()
        result = [intervals[0]]

        end = intervals[0][1]

        for i in range(1, len(intervals)):
            max_end = max(end, intervals[i][1])

            if intervals[i][0] <= end:
                result[-1][1] = max_end
            else:
                result.append(intervals[i])

            end = max_end

        return result

+ Recent posts