Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Merger Recursion solution in Clear category for Merge Intervals by oduvan
def merge_with(first, others):
if not others:
return [first]
second, others = others[0], others[1:]
if second[0] > first[1] + 1:
return [first] + merge_with(second, others)
return merge_with((min(first[0], second[1]), max(first[1], second[1])), others)
def merge_intervals(intervals):
"""
You have a collection of intervals. You should merge overloped intervals.
"""
if len(intervals) <=1:
return intervals
return merge_with(intervals[0], intervals[1:])
July 24, 2017
Comments: