easyIntervalsPure DSA~18 min
Maintenance Window Merge
A status page lists scheduled maintenance windows, but overlapping windows confuse users. Merge all overlapping or touching windows into the minimal set of non-overlapping intervals before publishing.
Problem
Given a list of intervals [start, end], merge all overlapping intervals (intervals that touch at an endpoint also merge) and return the merged list sorted by start.
Input
An array of n intervals (1 ≤ n ≤ 10^5), each [start, end] with 0 ≤ start ≤ end ≤ 10^9.
Output
An array of merged, non-overlapping intervals sorted ascending by start.
Constraints
- 1 ≤ n ≤ 100,000
- 0 ≤ start ≤ end ≤ 10^9
- Touching intervals (e.g. [1,3] and [3,5]) merge into one
Examples
Example 1
Input
intervals = [[1,3],[2,6],[8,10],[15,18]]
Output
[[1,6],[8,10],[15,18]]
[1,3] and [2,6] overlap → [1,6]. The other two do not overlap anything.
Example 2
Input
intervals = [[1,4],[4,5]]
Output
[[1,5]]
They touch at 4, so they merge into [1,5].