Asked at
Merge Intervals
MediumVerifiedArrayIntervalsSorting~20 min
Given an array of intervals where intervals[i] = [start, end], merge all overlapping intervals and return the result sorted ascending by start. Treat touching intervals (one’s end equals the next’s start) as overlapping.
The input arrives as a single array number[][].
Examples
in[[1, 3], [2, 6], [8, 10], [15, 18]]
out[[1, 6], [8, 10], [15, 18]]
[1,3] and [2,6] overlap, so they merge into [1,6].
in[[1, 4], [4, 5]]
out[[1, 5]]
Touching intervals at 4 merge into [1,5].
Constraints
- 1 ≤ intervals.length ≤ 10⁴
- intervals[i] == [start, end] with start ≤ end
- 0 ≤ start ≤ end ≤ 10⁴
Get help
🔑
Sign in to solve
Sign in to write, run, and submit your solution — and to pick up where your iOS flow left off.