Merge Intervals
Last updated
Last updated
Given an array of intervals
where intervals[i] = [starti, endi]
, merge all overlapping intervals, and return an array of the non-overlapping intervals that cover all the intervals in the input.
Example 1:
Example 2:
Constraints:
1 <= intervals.length <= 104
intervals[i].length == 2
0 <= starti <= endi <= 104
You can solve this problem by first sorting the intervals based on their start times and then merging overlapping intervals. Here's a JavaScript implementation:
This implementation sorts the intervals based on their start times. Then, it iterates through the sorted intervals and merges overlapping intervals. If the current interval overlaps with the last merged interval, it updates the end time of the last merged interval to the maximum of the current and last end times. Otherwise, it adds the current interval to the merged array. Finally, it returns the merged array of non-overlapping intervals.