← Practice
πŸ“– IntervalsLearning path
Browsing as guestΒ·Sign in or create a free account to save progress and use tutor tools.
LessonΒ·difficulty 3/5Β·~20 min

Intervals: merge overlapping ranges

You're given a list of intervals like [[1,3],[2,6],[8,10],[15,18]] and asked to merge every pair that overlaps (LeetCode #56). Here [1,3] and [2,6] touch, so they collapse into [1,6]; [8,10] and [15,18] stand alone. The result is [[1,6],[8,10],[15,18]].

The unlock is to sort the intervals by their start. Once they're sorted, overlaps can only happen between an interval and the one most recently merged β€” you never have to look backwards. Walk the sorted list keeping a "current" merged interval; if the next one starts at or before the current end, stretch the end, otherwise close out the current interval and start a new one.

In plain steps (you write the Python):

text
sort the intervals by their start
merged = [a copy of the first interval]
for each later (start, end):
    if start <= the end of the last merged interval:   # overlap β€” touching counts
        stretch that end to the larger of the two ends
    else:
        append [start, end] as a new, separate interval

Note that touching counts as overlapping: [1,4] and [4,5] merge into [1,5] because 4 <= 4.

Your task

Write merge(intervals) returning a list of [start, end] lists with all overlaps merged, sorted by start. Return plain lists (not tuples). An empty input gives an empty list.

python
merge([[1,3],[2,6],[8,10],[15,18]])   # [[1, 6], [8, 10], [15, 18]]
merge([[1,4],[4,5]])                   # [[1, 5]]   (touching merges)
merge([[1,4]])                         # [[1, 4]]
merge([])                              # []

Hint: sort first, then sweep once, growing the last merged interval whenever the next one starts at or before its end.

Tests

  • two overlaps
  • touching merges
  • single interval
  • empty input

✦ Stuck? Ask for a spark

Sign in or create a free account to use your monthly AI explainer allowance.

warming up
Loading editor…
Consolewaiting

$ Console output will appear here.

>_AI coach
Sign in or create a free account to chat with the tutor.