Count Active Surveillance Cameras at Each Minute
Greedy & IntervalsAuto-graded · 17 test casesLast updated
Tests the sweep-line technique by computing, for each minute in a query list, how many camera recording intervals are active at that exact moment.
Count Active Surveillance Cameras at Each Minute
A building's security system logs camera activity as a list of recording intervals. Each interval is represented as [start, end] (both inclusive), meaning that camera was actively recording from minute start through minute end.
Given a list of such recording intervals and a list of query minutes, return a list where the i-th element is the number of cameras that are actively recording at minute queries[i].
A camera is active at minute t if start <= t <= end.
Input
intervals: a list of[start, end]pairs, where eachstartandendis an integer.queries: a list of integers, each representing a minute to query.
Output
Return a list of integers of the same length as queries, where the i-th element is the count of intervals [start, end] such that start <= queries[i] <= end.
The output list must be in the same order as the input queries list.
Constraints
0 <= len(intervals) <= 20000 <= len(queries) <= 2000- For each interval
[start, end]:0 <= start <= end <= 10^6 0 <= queries[i] <= 10^6- Intervals may overlap and duplicate intervals are allowed.
queriesmay contain duplicate values; each is answered independently.
Examples
Example 1
Input: count_active_cameras([[1, 5], [2, 8], [6, 10]], [3, 6, 9])
Output: [2, 2, 1]
Example 2
Input: count_active_cameras([[0, 0], [0, 1], [1, 1]], [0, 1])
Output: [2, 2]
Example 3
Input: count_active_cameras([], [4, 7])
Output: [0, 0]
Hints
Hint 1
Sort the queries (while remembering their original positions) and process them in order — this lets you advance through interval events without re-scanning from scratch.
Hint 2
Build two sorted event lists: one for interval starts and one for interval ends. As you sweep through sorted query values, add cameras whose start <= current query and remove cameras whose end < current query.
Hint 3
Use two pointers — one into the sorted starts array and one into the sorted ends array — advancing each pointer as needed while you process each query in sorted order. Restore original query order at the end.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Key Insight: Sweep Line with Two Pointers
Rather than checking every interval for every query (O(Q × N)), we sort both the queries and the interval endpoints, then sweep through queries in order — advancing two pointers (one over sorted starts, one over sorted ends) as we go.
Steps:
-
Extract and sort starts and ends separately.
starts = sorted(s for s, e in intervals)
ends = sorted(e for s, e in intervals) -
Sort queries while tracking original indices so we can restore the original output order afterward.
-
Sweep: For each query
tin sorted order:- Advance the
startspointer: for everystarts[si] <= t, incrementactiveand advancesi. These cameras have begun recording by timet. - Advance the
endspointer: for everyends[ei] < t, decrementactiveand advanceei. These cameras finished recording before timet. - The current value of
activeis exactly the number of cameras whose interval containst.
- Advance the
-
Write the result into the position corresponding to the original query index.
Why This Is Correct
At any moment t, a camera is active iff start <= t AND end >= t, i.e., end is not < t. The sweep correctly maintains the count:
- Every interval with
start <= thas been counted intoactive. - Every interval with
end < thas been subtracted back out. - What remains is exactly the set of intervals straddling
t.
Because queries are processed in non-decreasing order, both pointers only move forward — they never need to backtrack.
Complexity
- Time: O(N log N + Q log Q) — dominated by sorting intervals and queries.
- Space: O(N + Q) — for the sorted starts/ends arrays and the indexed queries list.
Reference Solution
from typing import List
def count_active_cameras(intervals: list, queries: list) -> list:
if not queries:
return []
starts = sorted(s for s, e in intervals)
ends = sorted(e for s, e in intervals)
indexed_queries = sorted(enumerate(queries), key=lambda x: x[1])
result = [0] * len(queries)
active = 0
si = 0
ei = 0
for orig_idx, t in indexed_queries:
while si < len(starts) and starts[si] <= t:
active += 1
si += 1
while ei < len(ends) and ends[ei] < t:
active -= 1
ei += 1
result[orig_idx] = active
return result