An interval is a [start, end] pair. Many calendar and scheduling problems begin by collapsing a messy list of intervals into a clean, non-overlapping one.
Implement merge_intervals(intervals). Sort the intervals by start time, then sweep left to right merging any interval that overlaps (or touches) the one before it. Two intervals overlap when the next start is less than or equal to the current end.
Return a new list of [start, end] pairs sorted by start. An empty input returns an empty list.
def merge_intervals(intervals):
"""Sort intervals by start and merge overlapping ones.
intervals is a list of [start, end] pairs. Return the merged
intervals as a list of [start, end] pairs sorted by start.
"""
# TODO: sort by start, then sweep and merge intervals that overlap
passThe editor and the test runner need a wide screen. Open this page on a desktop browser and your progress will be waiting.
An interval is a [start, end] pair. Many calendar and scheduling problems begin by collapsing a messy list of intervals into a clean, non-overlapping one.
Implement merge_intervals(intervals). Sort the intervals by start time, then sweep left to right merging any interval that overlaps (or touches) the one before it. Two intervals overlap when the next start is less than or equal to the current end.
Return a new list of [start, end] pairs sorted by start. An empty input returns an empty list.
Press Run Tests or ββ© to check your solution.