Merge Intervals
Merge Intervals
Given an array of intervals intervals where intervals[i] = [start, end], merge all overlapping intervals and return an array of the non-overlapping intervals that cover all intervals in the input.
Function signature
func MergeIntervals(intervals [][]int) [][]int
Example
intervals = [[1,3],[2,6],[8,10],[15,18]]
output = [[1,6],[8,10],[15,18]]
Constraints
- 0 <= len(intervals) <= 100_000
- intervals[i] has length 2
- -1_000_000_000 <= start <= end <= 1_000_000_000
Notes
- Sort by start, then sweep and merge in O(n log n).
Run tests to see results
No issues detected