[LeetCode][Python3] 56. Merge Intervals
                2019. 4. 3. 02:01 |
                
                    프로그래밍/LeetCode
                
            
            
            
        Problem :
https://leetcode.com/problems/merge-intervals/
My Solution :
# Definition for an interval.
# class Interval:
#     def __init__(self, s=0, e=0):
#         self.start = s
#         self.end = e
class Solution:
    def merge(self, intervals: List[Interval]) -> List[Interval]:
        intervals = sorted(intervals, key=lambda x:x.start)
        ret = []
        for node in intervals:
            if not ret or ret[-1].end < node.start:
                ret.append(node)
            elif ret[-1].end < node.end:
                ret[-1].end = node.end
        return ret
Comment :
start 기준으로 먼저 정렬을 해두면 merge 하기 쉽다.
'프로그래밍 > LeetCode' 카테고리의 다른 글
| [LeetCode][Python3] 23. Merge k Sorted Lists (0) | 2019.04.07 | 
|---|---|
| [LeetCode][Python3] 210. Course Schedule II (0) | 2019.04.05 | 
| [LeetCode][Python3] 148. Sort List (1) | 2019.04.04 | 
| [LeetCode][Python3] 139. Word Break (0) | 2019.04.03 | 
| [LeetCode][Python3] 295. Find Median from Data Stream (0) | 2019.04.02 | 
| [LeetCode][Python3] 236. Lowest Common Ancestor of a Binary Tree (0) | 2019.04.01 | 
| [LeetCode][Python3] 207. Course Schedule (0) | 2019.03.31 | 
| [LeetCode][Python3] 116. Populating Next Right Pointers in Each Node (0) | 2019.03.30 | 
최근에 달린 댓글 최근에 달린 댓글