Problem · Array
Merge Intervals
Learn this problemProblem statement
You are given an array of intervals where intervals[i] = [start_i, end_i].
Merge every pair of overlapping intervals and return an array of non-overlapping intervals that covers all intervals in the input.
Two intervals overlap if they share at least one common point. For example, [1, 2] and [3, 4] do not overlap, but [1, 2] and [2, 3] do overlap.
You may return the merged intervals in any order.
Function
mergeIntervals(intervals: int[][]) → int[][]Examples
Example 1
intervals = [[1, 3], [1, 5], [6, 7]]return = [[1, 5], [6, 7]]The intervals [1, 3] and [1, 5] overlap, so they merge into [1, 5]. The interval [6, 7] does not overlap with that merged interval.
Example 2
intervals = [[1, 2], [2, 3]]return = [[1, 3]]The two intervals share point 2, so they overlap and merge into [1, 3].
Constraints
1 <= intervals.length <= 1000intervals[i].length == 20 <= start_i <= end_i <= 1000