call(back)
Algorithms & Data StructuresmediumSign in to star problems

Merge Overlapping Intervals

Given a list of intervals [start, end], merge all overlapping intervals and return the result sorted by start time. Intervals that touch, such as [1, 4] and [4, 5], overlap.

Hints

Hint 1

Sort by start time first. What invariant does that buy you when you sweep left to right?

Hint 2

An interval overlaps the last merged one exactly when its start is less than or equal to the last merged end.

Asked at