Largest Group Anchored by One Overlapping Interval
Greedy & IntervalsAuto-graded · 22 test casesLast updated
Find the largest set of intervals that all intersect one chosen anchor interval, using sorting and binary search to beat quadratic time.
A company has n employees. Employee i is available during the inclusive time window [start_time[i], end_time[i]], so the arrays start_time and end_time are aligned by index.
You want to assemble a group that contains an anchor employee. The anchor's window must intersect the window of every other member of the group. Windows [a, b] and [c, d] intersect when they share at least one time point, that is, when max(a, c) <= min(b, d). Touching at a single endpoint counts as intersecting.
Two members who are not the anchor are not required to intersect each other. Only their relationship with the anchor matters.
Return the largest possible size of such a group, with the anchor counted as a member. If there are no employees, return 0.
The solution should run faster than O(n^2).
Constraints
0 <= n <= 10^5, wheren = len(start_time) = len(end_time)start_time[i] <= end_time[i]for everyi- Time values are integers and may be large or negative
- Windows may be identical or share endpoints
Examples
Example 1
Input: max_anchored_group([2, 5, 6, 8], [5, 6, 10, 9])
Output: 3
Example 2
Input: max_anchored_group([1, 4, 7], [3, 6, 9])
Output: 1
Example 3
Input: max_anchored_group([0, 0, 0], [0, 0, 0])
Output: 3
Hints
Hint 1
For a fixed anchor, nothing limits the other members except that they overlap the anchor, so the best group is every window that overlaps it (the anchor included). Then maximize over anchors.
Hint 2
Counting overlaps directly for each anchor is quadratic. Count the windows that do NOT overlap instead: those that end strictly before the anchor starts, and those that start strictly after the anchor ends.
Hint 3
These two failing sets can never share a window, so you can subtract their sizes from n. Sort the starts and the ends once and use binary search, taking care with strict versus non-strict comparisons at the endpoints.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Fix a candidate anchor with window [s, e]. Any window that intersects it may join, and no other restrictions exist, so the best group for this anchor is all windows that intersect [s, e], including the anchor itself. The answer is the maximum of this count over all anchors.
Counting overlaps one anchor at a time is O(n) each, so O(n^2) overall. Instead, count the windows that fail to intersect. A window [a, b] misses [s, e] in exactly one of two ways:
- It ends strictly before the anchor starts:
b < s. - It starts strictly after the anchor ends:
a > e.
So overlap = n - #{b < s} - #{a > e}.
Sort all starts and all ends once. Then:
#{a > e} = n - bisect_right(sorted_starts, e)#{b < s} = bisect_left(sorted_ends, s)
This gives overlap = bisect_right(sorted_starts, e) + (n - bisect_left(sorted_ends, s)) - n.
Why it is correct
- Intersection test. Windows intersect iff
c <= banda <= d. Its negation is exactly "ends before" or "starts after", which is why the comparisons are strict and shared endpoints count as overlap. - Disjoint failure sets. A window with both
b < sanda > ewould satisfya > e >= s > b, contradictinga <= b. So nothing is subtracted twice. - The anchor counts itself. Its own window is in neither failing set.
- Optimality. Any valid group has some anchor, and all its members lie in that anchor's overlap set, so its size is at most that count. Taking the full set achieves the bound.
A sweep-line "maximum coverage at one point" is the wrong model. An anchor [1, 10] can overlap [1, 2] and [9, 10], which never share a point with each other, giving a group of 3 although the maximum point coverage is 2.
Complexity
- Time:
O(n log n)for two sorts plus two binary searches per employee. - Space:
O(n)for the sorted copies.
Reference solution
from bisect import bisect_left, bisect_right
from typing import List
def max_anchored_group(start_time: List[int], end_time: List[int]) -> int:
n = len(start_time)
if n == 0:
return 0
sorted_starts = sorted(start_time)
sorted_ends = sorted(end_time)
best = 0
for s, e in zip(start_time, end_time):
# windows starting at or before e
starts_before_end = bisect_right(sorted_starts, e)
# windows ending at or after s
ends_after_start = n - bisect_left(sorted_ends, s)
# overlap count = n - (start > e) - (end < s)
overlap = starts_before_end + ends_after_start - n
if overlap > best:
best = overlap
return best