소프트웨어에 대한 모든 것

LeetCode 풀기 - 56. Merge Intervals 본문

알고리즘/LeetCode

LeetCode 풀기 - 56. Merge Intervals

앤테바 2021. 12. 24. 16:22
반응형

56. Merge Intervals

https://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

문제)

솔루션1) straight forward

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

 

반응형
Comments