본문 바로가기
Problem Solving/리트코드

[리드코드 leetcode] 56. Merge Intervals

by ggyongi 2021. 4. 14.
반응형

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

 

 

비전공자 네카라 신입 취업 노하우

시행착오 끝에 얻어낸 취업 노하우가 모두 담긴 전자책!

kmong.com

댓글