Problem Solving/리트코드

[리드코드 leetcode] 56. Merge Intervals

ggyongi 2021. 4. 14. 17:13

leetcode.com/problems/merge-intervals/

 

Merge Intervals - LeetCode

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

풀긴 하였으나 최선은 아닌 것 같다..

class Solution:
    def merge(self, intervals: List[List[int]]) -> List[List[int]]:
        
        intervals.sort(key = lambda x: x[0])
        
        result = []
        temp = intervals[0]
        
        for i in range(1, len(intervals)):
            if temp[1] >= intervals[i][0]:
                temp[1] = max(temp[1],intervals[i][1])  
            else:
                result.append(temp)
                temp = intervals[i]
           
        result.append(temp)
        return result