Shared High-Activity Windows Across Sensor Feeds
Greedy & IntervalsAuto-graded · 21 test casesLast updated
Merge step-function sensor feeds with a sweep over status-change events to find every maximal inclusive window in which all feeds are at or above a level.
Shared High-Activity Windows Across Sensor Feeds
You are given several sensor feeds. Each feed is a list of [timestamp, level] readings, sorted by strictly increasing integer timestamp. Levels are integers in 0..1000000, standing for fixed-point values 0.000000 to 1.000000. The cutoff min_level uses the same scale.
How a feed behaves over time
- Time is made of integer timestamps only.
- A reading at timestamp
tstays in effect for every integer timestamp fromtup to, but not including, the next reading's timestamp. - A feed's last reading stays in effect through
last_time, inclusive. - A feed has no defined level before its first reading. Analysis therefore begins at the latest first-reading timestamp across all feeds. Earlier timestamps are never reported.
Task
Return every maximal inclusive window [start, end] in which, at every integer timestamp, every feed has an effective level that is at least min_level.
- Windows must be sorted by
start. - Windows must not overlap or touch. If two qualifying stretches are adjacent (one ends at
xand the next starts atx + 1), they form a single window. - A window may be a single timestamp, such as
[8, 8]. - If no timestamp qualifies, return an empty list.
- With one feed, the task reduces to finding that feed's own qualifying windows.
Function
shared_activity_windows(feeds: List[List[List[int]]], min_level: int, last_time: int) -> List[List[int]]
feeds: the list of feeds, each a list of[timestamp, level]readings.min_level: the cutoff level.last_time: the final timestamp, inclusive, through which each feed's last reading stays in effect.
Constraints
1 <= len(feeds) <= 1000- Every feed has at least one reading, and its timestamps are strictly increasing.
- Every reading timestamp lies in
[0, last_time]. 0 <= min_level <= 10000000 <= last_time <= 10^12- The total number of readings across all feeds is at most
200000.
Examples
Example 1
Input: shared_activity_windows([[[1, 200000], [3, 700000], [6, 800000], [8, 100000]]], 600000, 10)
Output: [[3, 7]]
Example 2
Input: shared_activity_windows([[[0, 800000], [5, 300000], [8, 900000]], [[2, 700000], [6, 900000], [9, 200000]]], 600000, 10)
Output: [[2, 4], [8, 8]]
Hints
Hint 1
Only whether a feed is at or above the cutoff matters, not its exact level. Turn each feed into a yes/no step function and look only at the moments where the answer flips.
Hint 2
Do not walk through every timestamp, since last_time can reach 10^12. Record +1 or -1 at each timestamp where a feed's status flips, then sweep over the sorted event timestamps while counting how many feeds currently qualify.
Hint 3
At the analysis start, use the reading in effect there (found by binary search), not the first reading. A window is open exactly while the count equals the number of feeds. It closes at event_time - 1 on a drop and, if still open at the end, at last_time. Sum the changes at equal timestamps before acting on them.
Worked solution
Hard Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Only whether a feed is at or above min_level matters, so each feed becomes a yes/no step function. We want the timestamps at which all feeds say yes at once.
- Find the start.
beginis the maximum of all first-reading timestamps. Before it, some feed has no defined level. - Find each feed's initial status. Binary search for the last reading with timestamp
<= begin. That reading is in effect atbegin. Count the feeds that qualify at that moment. - Emit events only on status flips. Walk the later readings of each feed. When the status changes, add
+1(starts qualifying) or-1(stops) at that reading's timestamp in a map. A reading that changes the level but not the status produces no event, so it can never split a window. - Sweep. Process the event timestamps in sorted order, tracking how many feeds currently qualify.
- If a window is open and a nonzero net change arrives, the change must be negative. The window closes at
t - 1. - Apply the change. If all feeds now qualify, open a new window at
t. - Net-zero timestamps are skipped, because the set of qualifying feeds has not effectively changed in count.
- If a window is open and a nonzero net change arrives, the change must be negative. The window closes at
- Close at the horizon. A window still open after the last event ends at
last_time.
Why it is correct
- Between consecutive event timestamps no feed changes status, so the qualifying count is constant. All feeds qualify on that stretch exactly when the count equals the number of feeds.
- Using the reading in effect at
beginhandles earlier readings correctly. Only the latest reading at or beforebeginmatters, and every later reading has a timestamp strictly greater thanbegin. - Summing changes per timestamp makes simultaneous changes atomic. A net-zero change while all feeds qualify cannot occur, because no feed could be rising. So a window never closes and reopens at the same timestamp.
- A window closes at
t - 1only when the count drops att, so there is a gap of at least one timestamp before any later window. Adjacent qualifying stretches are therefore never split, which keeps windows maximal and non-touching. - The time range is never materialized, which matters since
last_timecan reach10^12.
Complexity
Let R be the total number of readings.
- Time:
O(R log R), dominated by sorting the distinct event timestamps. Binary searches and event generation areO(R)overall. - Space:
O(R)for the timestamp lists, the event map and the output.
Reference solution
from bisect import bisect_right
from collections import defaultdict
from typing import List
def shared_activity_windows(feeds: List[List[List[int]]], min_level: int, last_time: int) -> List[List[int]]:
begin = max(feed[0][0] for feed in feeds)
total = len(feeds)
changes = defaultdict(int)
qualifying_now = 0
for feed in feeds:
times = [r[0] for r in feed]
idx = bisect_right(times, begin) - 1
ok = feed[idx][1] >= min_level
if ok:
qualifying_now += 1
for j in range(idx + 1, len(feed)):
next_ok = feed[j][1] >= min_level
if next_ok != ok:
changes[feed[j][0]] += 1 if next_ok else -1
ok = next_ok
windows = []
open_start = begin if qualifying_now == total else None
for t in sorted(changes):
delta = changes[t]
if delta == 0:
continue
if qualifying_now == total and open_start is not None:
if t - 1 >= open_start:
windows.append([open_start, t - 1])
open_start = None
qualifying_now += delta
if qualifying_now == total:
open_start = t
if qualifying_now == total and open_start is not None:
windows.append([open_start, last_time])
return windows