링크
겹치는 범위를 하나로 합치기
Input: intervals = [[1,3],[2,6],[8,10],[15,18]]
Output: [[1,6],[8,10],[15,18]]
def merge(self, intervals: List[List[int]]) -> List[List[int]]:
intervals.sort()
ans = []
prev = intervals[0]
n = len(intervals)
for i in range(n):
inter = intervals[i]
if inter[0] <= prev[1]:
prev[1] = max(prev[1], inter[1])
else:
ans.append(prev)
prev = inter
if i == n - 1: ans.append(prev)
return ans
----- 마지막 index 고려할 필요 X ----
def merge(self, intervals: List[List[int]]) -> List[List[int]]:
intervals.sort()
ans = [intervals[0]]
n = len(intervals)
for i in range(n):
inter = intervals[i]
if inter[0] <= ans[-1][1]:
ans[-1][1] = max(ans[-1][1], inter[1])
else:
ans.append(inter)
return ans