Track Segment Coverage by Expanding Intervals
Greedy & IntervalsAuto-graded · 24 test casesLast updated
Tests interval merging and coverage detection with streaming data updates.
You are monitoring a one-dimensional segment from 0 to 50.
You will receive a sequence of floating-point positions. Each position x affects a region extending 0.5 units in both directions: specifically, the closed interval [x - 0.5, x + 0.5]. Once a portion of the segment is affected, it remains affected permanently. Effects from different positions can overlap and combine.
Write a function that processes each position in order and returns a list of boolean values. For each position, the corresponding boolean indicates whether the entire segment [0, 50] is completely affected after processing that position (and all previous positions).
Important details:
- Intervals are closed (endpoints included). Two intervals that share an endpoint (like
[1, 2]and[2, 3]) cover the shared point without gaps. - Only the portion of each interval that overlaps with
[0, 50]matters. A position outside the range[-0.5, 50.5]contributes nothing. - Positions arrive in the order given and must be processed sequentially.
- Once the segment is fully covered, it remains fully covered for all subsequent positions.
Constraints:
1 <= len(points) <= 10^4-10^6 <= points[i] <= 10^6- All inputs are finite floating-point numbers
Examples
Example 1
Input: track_segment_coverage([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50])
Output: [False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, True]
Example 2
Input: track_segment_coverage([25, 0, 50, 1, 49])
Output: [False, False, False, False, False]
Example 3
Input: track_segment_coverage([100, -100, 25, 0, 50])
Output: [False, False, False, False, False]
Example 4
Input: track_segment_coverage([0.5, 49.5, 25, 10, 40])
Output: [False, False, False, False, False]
Hints
Hint 1
Maintain a collection of disjoint intervals representing affected regions, merging overlapping or touching intervals as new positions arrive.
Hint 2
Use binary search to efficiently find which existing intervals overlap or touch the new interval created by each position.
Hint 3
The segment is fully covered when exactly one interval remains and it spans from 0 to 50.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
The key insight is to maintain a sorted list of disjoint, non-overlapping intervals representing all affected regions. When a new position arrives, we:
- Compute its affected interval
[x - 0.5, x + 0.5]and clip it to[0, 50] - Find all existing intervals that overlap or touch this new interval
- Merge them all into a single consolidated interval
- Check if we now have exactly one interval spanning
[0, 50]
To efficiently find overlapping intervals, we use binary search. Since our intervals are sorted and disjoint, any intervals that overlap with [left, right] form a contiguous block in our list.
The search works as follows:
- Use
bisect_leftto find where the new interval would be inserted based on its left endpoint - Check if the previous interval extends far enough right to touch our new interval
- Merge forward through all intervals whose left endpoint is within our new interval's right endpoint
Once intervals are merged, we check for complete coverage: this occurs when we have a single interval with left endpoint ≤ 0 and right endpoint ≥ 50.
Why It Works
The algorithm maintains the invariant that stored intervals are always sorted by position and never overlap or touch each other. When a new interval arrives, any stored interval that overlaps or touches it must be merged into one larger interval. Since intervals are sorted, all such intervals form a contiguous subsequence that we can find and replace efficiently.
The coverage check is exact because the target segment [0, 50] is a connected region. It can only be fully covered by a single merged interval that spans its entire length.
Complexity
Time: O(n × k) where n is the number of positions and k is the number of stored intervals. In the worst case, each update involves list operations costing O(k). However, k is bounded by the segment length divided by the minimum interval size, giving k ≤ 51 for this problem, making each operation effectively O(1). Amortized over all merges, the total work is O(n).
Space: O(k) for storing the disjoint intervals, which is O(1) for this fixed-size segment.
from bisect import bisect_left
from typing import List
def track_segment_coverage(points: List[float]) -> List[bool]:
intervals = []
results = []
complete = False
for x in points:
if complete:
results.append(True)
continue
left = max(0.0, x - 0.5)
right = min(50.0, x + 0.5)
if left <= right:
index = bisect_left(intervals, [left])
if index > 0 and intervals[index - 1][1] >= left:
index -= 1
left = min(left, intervals[index][0])
right = max(right, intervals[index][1])
intervals.pop(index)
while index < len(intervals) and intervals[index][0] <= right:
left = min(left, intervals[index][0])
right = max(right, intervals[index][1])
intervals.pop(index)
intervals.insert(index, [left, right])
complete = bool(intervals and intervals[0][0] <= 0.0 and intervals[0][1] >= 50.0)
results.append(complete)
return results