Sort intervals by its start point first. The iterate the interval list and merge intervals overlaps.
from operator import itemgetter
class Solution:
def merge(self, intervals: List[List[int]]) -> List[List[int]]:
intervals.sort()
result = []
for start, end in intervals:
if result and result[-1][0] <= start <= result[-1][1]:
result[-1][0] = min(result[-1][0], start)
result[-1][1] = max(result[-1][1], end)
else:
result.append([start, end])
return result
No comments:
Post a Comment