← 返回 meta 的题目列表Merge Intervals
类型:online_judge
Given an array of intervals where each interval is [start, end], merge all overlapping intervals, and return an array of the non-overlapping intervals that cover all the intervals in the input.
Input
A 2D array intervals, where intervals[i] = [start_i, end_i].
Output
A 2D array representing the merged intervals.
Example
Input: intervals = [[1,3],[2,6],[8,10],[15,18]]
Output: [[1,6],[8,10],[15,18]]
Input: intervals = [[1,4],[4,5]]
Output: [[1,5]]
Constraints
intervals[i][0] <= intervals[i][1]
The number of intervals is in the range [1, 10^4].
Complexity
Expected time complexity: O(n log n), where n is the number of intervals
Expected space complexity: O(n)
Example
Input
[[1,3],[2,6],[8,10],[15,18]]