Find the Maximum Size Connected Interval Component
Greedy & IntervalsAuto-graded · 23 test casesLast updated
Find the maximum number of intervals that belong to a single connected component where edges represent inclusive interval overlaps.
Given a collection of inclusive integer intervals where each interval is represented as a list or tuple [start, end], determine the size of the largest connected group of intervals.
Two intervals [start_a, end_a] and [start_b, end_b] directly connect if they overlap, meaning:
$$\max(start_a, start_b) \le \min(end_a, end_b)$$
A set of intervals forms a connected component if every interval in the component can reach any other interval in the component through a chain of overlapping intervals.
Return the maximum number of intervals contained within any single connected component. If the input list is empty, return 0.
Constraints
0 <= len(intervals) <= 2 * 10^5- Each interval contains exactly two integers:
[start, end]withstart <= end. startandendvalues can be negative, zero, or positive.
Examples
Example 1
Input: largest_overlapping_group([[1, 3], [2, 5], [6, 8]])
Output: 2
Example 2
Input: largest_overlapping_group([[1, 2], [2, 4], [4, 6]])
Output: 3
Example 3
Input: largest_overlapping_group([[1, 10], [2, 3], [4, 5], [11, 12]])
Output: 3
Hints
Hint 1
Building an explicit adjacency list and running BFS/DFS will be too slow ($O(n^2)$ edges in the worst case).
Hint 2
Sort the intervals by their start time. What does this reveal about how connected components map to contiguous segments of the sorted array?
Hint 3
Maintain a running maximum end coordinate for the current group. If a new interval starts after this maximum end coordinate, a component boundary has been reached.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Method Explanation
Constructing an explicit graph with edges between every overlapping pair can take up to $\mathcal{O}(n^2)$ time, which is too slow for $n = 2 \cdot 10^5$.
Instead, we can exploit the geometric property of intervals on a 1D line:
- Contiguous Components: If we sort intervals primarily by their starting points, any connected component corresponds to a contiguous subsegment of the sorted array.
- Boundary Condition: While iterating through the sorted intervals, we track
max_reach, which is the maximumendpoint encountered so far in the current component. - If an interval has
start > max_reach, it cannot overlap with any interval seen so far. Furthermore, since all subsequent intervals will have an even larger or equalstart, no future interval can connect back to the current group either. Thus, the current component is complete, and we can finalize its count and start a new component. - If
start <= max_reach, the interval overlaps with at least one previously seen interval in the current group. It joins the group, and we updatemax_reach = max(max_reach, end).
Complexity Analysis
- Time Complexity: $\mathcal{O}(n \log n)$ to sort the intervals, followed by a single linear scan taking $\mathcal{O}(n)$ time. Overall time complexity is $\mathcal{O}(n \log n)$.
- Space Complexity: $\mathcal{O}(n)$ to store the sorted array (or $\mathcal{O}(1)$ extra space beyond sorting overhead if done in-place).
Python Source Code
from typing import List
def largest_overlapping_group(intervals: List[List[int]]) -> int:
if not intervals:
return 0
sorted_intervals = sorted(intervals)
max_size = 0
current_size = 0
max_reach = None
for start, end in sorted_intervals:
if max_reach is None or start > max_reach:
max_size = max(max_size, current_size)
current_size = 1
max_reach = end
else:
current_size += 1
max_reach = max(max_reach, end)
return max(max_size, current_size)