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 하기 쉽다.