Merge Intervals
Problem Statement
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.
Constraints:
- 1 <= intervals.length <= 10^4
- intervals[i].length == 2
- 0 <= starti <= endi <= 10^4
Input Format:
- An array of intervals where intervals[i] = [starti, endi]
Output Format:
- An array of the non-overlapping intervals that cover all the intervals in the input
Examples:
Example 1:
Input:
intervals = [[1,3],[2,6],[8,10],[15,18]]
Output:
[[1,6],[8,10],[15,18]]
Explanation:
Since intervals [1,3] and [2,6] overlap, merge them into [1,6].
Example 2:
Input:
intervals = [[1,4],[4,5]]
Output:
[[1,5]]
Explanation:
Intervals [1,4] and [4,5] are considered overlapping since they share an endpoint.
Example 3:
Input:
intervals = [[1,4],[0,4]]
Output:
[[0,4]]
Explanation:
Interval [0,4] covers interval [1,4], so they are merged.
Solutions
Sort and Merge
Sort the intervals by start time, then iterate through the intervals, merging overlapping ones.
Line Sweep Algorithm
Keep track of the start and end points separately, then perform a line sweep to determine the merged intervals.
Algorithm Walkthrough
Example input:
Step-by-step execution:
- intervals = [[1,3],[2,6],[8,10],[15,18]]
- Sort by start time: [[1,3],[2,6],[8,10],[15,18]]
- Initialize result = [[1,3]]
- i=1: intervals[1] = [2,6]
- Last merged interval = [1,3]
- Since 2 <= 3, there is an overlap, merge to [1,6]
- result = [[1,6]]
- i=2: intervals[2] = [8,10]
- Last merged interval = [1,6]
- Since 8 > 6, no overlap, add to result
- result = [[1,6],[8,10]]
- i=3: intervals[3] = [15,18]
- Last merged interval = [8,10]
- Since 15 > 10, no overlap, add to result
- result = [[1,6],[8,10],[15,18]]
- Return result = [[1,6],[8,10],[15,18]]
Hints
Hint 1
Hint 2
Hint 3
Video Tutorial
Video tutorials can be a great way to understand algorithms visually
Visualization
Visualize the intervals on a number line and how they merge as the algorithm progresses.
Key visualization elements:
- current interval
- merged interval
- result after each step
Similar Questions
Insert Interval
MediumYou are given an array of non-overlapping intervals intervals where intervals[i] = [starti, endi] represent the start and the end of the ith interval and intervals is sorted in ascending order by starti. You are also given an interval newInterval = [start, end] that represents the start and end of another interval. Insert newInterval into intervals such that intervals is still sorted in ascending order by starti and intervals still does not have any overlapping intervals (merge overlapping intervals if necessary). Return intervals after the insertion.
Meeting Rooms
EasyGiven an array of meeting time intervals where intervals[i] = [starti, endi], determine if a person could attend all meetings. In other words, check if there are any overlapping intervals.
Meeting Rooms II
MediumGiven an array of meeting time intervals where intervals[i] = [starti, endi], find the minimum number of conference rooms required to hold all meetings.
Implementation Notes
This is a classic interval problem. The key insight is to sort the intervals first, which makes the merging process much simpler.