#184MediumGreedy· Merging Intervals
Merge Maintenance Windows
Input format
`windows`: an array of pairs `[start, end]` with `start <= end`.
Output format
An array of merged, non-overlapping windows sorted by start.
Examples
Input: windows = [[1, 3], [8, 10], [2, 6], [15, 18]]
Output: [[1, 6], [8, 10], [15, 18]]
[1, 3] and [2, 6] overlap and become [1, 6]. [8, 10] and [15, 18] stand alone.
Input: windows = [[1, 4], [4, 5]]
Output: [[1, 5]]
The windows share minute 4, so they are merged into [1, 5].
Input: windows = [[5, 6], [1, 10], [2, 3]]
Output: [[1, 10]]
The window [1, 10] contains both of the others, so only [1, 10] remains.
Constraints
- 1 <= windows.length <= 10^4
- 0 <= start <= end <= 10^6
Study first
- #intervals
- #sorting
- #greedy
- #arrays