Combine Overlapping Time Intervals
Greedy & IntervalsAuto-graded · 20 test casesLast updated
Consolidate overlapping and adjacent closed intervals into a minimal list of disjoint intervals using sorting.
You are given a list of 2D integer arrays intervals where intervals[i] = [start_i, end_i] represents a closed numerical interval starting at start_i and ending at end_i.
Combine all overlapping or touching intervals so that the output contains no overlapping regions and completely covers all original inputs.
Two intervals [a, b] and [c, d] overlap or touch if $c \le b$ (assuming $a \le c$). In particular, intervals that share an endpoint (e.g., [1, 4] and [4, 5]) are considered overlapping and must be merged into a single interval [1, 5].
Return a list of non-overlapping intervals sorted by their starting point.
Constraints
- $1 \le \text{intervals.length} \le 10^4$
- $\text{intervals}[i].\text{length} == 2$
- $0 \le start_i \le end_i \le 10^9$
Examples
Example 1
Input: merge_intervals([[1, 3], [2, 6], [8, 10], [15, 18]])
Output: [[1, 6], [8, 10], [15, 18]]
Example 2
Input: merge_intervals([[1, 4], [4, 5]])
Output: [[1, 5]]
Hints
Hint 1
Sorting the intervals by their starting value simplifies identifying overlaps to adjacent comparisons.
Hint 2
Iterate through the sorted intervals while keeping track of the current active interval.
Hint 3
If the next interval begins before or at the end of the active interval, extend the active interval's end using the maximum of both end points.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
To consolidate arbitrary intervals efficiently, we sort the intervals in ascending order by their start point.
Sorting ensures that if an interval overlaps with any previously processed interval, it must overlap with the most recently formed interval in our output list:
- Sort: Order all intervals by their starting position
intervals[i][0]. - Initialize: Start our output list
mergedwith a deep copy of the first interval. - Iterate & Merge: For each subsequent interval
[start, end]:- If
start <= merged[-1][1], the interval overlaps or touches the previous one. We update the upper bound ofmerged[-1]tomax(merged[-1][1], end)to cover both. Taking the maximum accounts for completely enclosed intervals (e.g.,[2, 5]inside[1, 10]). - If
start > merged[-1][1], there is a gap. Append[start, end]as a new interval tomerged.
- If
Complexity
- Time Complexity: $\mathcal{O}(N \log N)$ where $N$ is the number of intervals, dominated by the sorting step. The linear scan takes $\mathcal{O}(N)$ time.
- Space Complexity: $\mathcal{O}(N)$ auxiliary space to store the output list and accommodate sorting overhead.
Python Reference Solution
from typing import List
def merge_intervals(intervals: List[List[int]]) -> List[List[int]]:
if not intervals:
return []
# Sort intervals primarily by start boundary
ordered = sorted(intervals, key=lambda x: x[0])
merged = [[ordered[0][0], ordered[0][1]]]
for start, end in ordered[1:]:
# Check if current interval overlaps or touches the previous one
if start <= merged[-1][1]:
merged[-1][1] = max(merged[-1][1], end)
else:
merged.append([start, end])
return merged