Extract Continuous Active Time Windows
Greedy & IntervalsAuto-graded · 18 test casesLast updated
Process chronological sensor sample logs to extract continuous active periods matching a minimum elapsed duration requirement.
You are given a chronological sequence of system readings represented as a list of pairs [timestamp, state].
timestampis a strictly increasing integer time value.stateis an integer indicating whether the system is active (1) or inactive (0).
A continuous active period consists of a uninterrupted sequence of 1 states. The time interval of an active period starts at the timestamp of the first 1 in the sequence and ends at the timestamp of the last 1 in that sequence.
The duration of an interval [t_start, t_end] is computed as t_end - t_start. Note that a sequence containing only a single active sample has t_start == t_end and a duration of 0.
Your goal is to extract all active time intervals whose total duration t_end - t_start is at least k time units (duration >= k).
Return the qualifying intervals as a list of [start_timestamp, end_timestamp] pairs in the order they occur.
Constraints
0 <= len(samples) <= 10^5- Each element of
samplesis a list of two integers[timestamp, state]. - Timestamps are strictly increasing integers.
stateis either0or1.0 <= k <= 10^9
Examples
Example 1
Input: get_active_intervals([[1, 1], [2, 1], [3, 0], [4, 1], [5, 1], [6, 1]], 2)
Output: [[4, 6]]
Example 2
Input: get_active_intervals([[1, 0], [5, 1]], 0)
Output: [[5, 5]]
Hints
Hint 1
Iterate through the samples maintaining tracking variables for the start timestamp and most recent timestamp of the current active run.
Hint 2
When encountering a zero or reaching the end of the list, compute the duration of the current active run (if one is open).
Hint 3
Make sure to evaluate and possibly append the final active run after the main loop finishes.
Worked solution
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Approach
We can find all active intervals in a single linear scan over the dataset.
- Maintain two tracking pointers,
startandend, initially set toNone. - As we iterate through each
[timestamp, state]pair:- If
state == 1:- If
startisNone, this is the beginning of a new active block; recordstart = timestamp. - Update
end = timestampto extend the active block.
- If
- If
state == 0and an active block is currently open (start is not None):- Calculate its duration:
end - start. - If
end - start >= k, append[start, end]to our result list. - Reset
startandendtoNone.
- Calculate its duration:
- If
- After completing the loop, check if an active block was still open at the end of the list. If so, check its duration
end - start >= kand append[start, end]if it meets the requirement.
Complexity
- Time Complexity: $\mathcal{O}(N)$, where $N$ is the number of samples. Each sample is processed exactly once in constant time $\mathcal{O}(1)$.
- Space Complexity: $\mathcal{O}(1)$ auxiliary space (excluding the memory required to store the output list).
Python Reference Solution
from typing import List
def get_active_intervals(samples: List[List[int]], k: int) -> List[List[int]]:
intervals = []
start = None
end = None
for timestamp, state in samples:
if state == 1:
if start is None:
start = timestamp
end = timestamp
elif start is not None:
if end - start >= k:
intervals.append([start, end])
start = None
end = None
if start is not None and end - start >= k:
intervals.append([start, end])
return intervals