Count Peak Hours Across Venue Time Slots
Greedy & IntervalsAuto-graded · 15 test casesLast updated
Tests the ability to use a sweep line (difference array) to efficiently count how many time slots are simultaneously "active" and find the maximum overlap count.
Count Peak Hours Across Venue Time Slots
A venue logs a list of time slots when it is in use. Each time slot is represented as a half-open interval [start, end) measured in integer minutes from midnight (so a slot from minute 60 to minute 120 covers minutes 60, 61, …, 119 but not 120).
Given a list of such intervals, find the maximum number of intervals that are simultaneously active at any single minute, and also return the earliest minute at which that maximum is first achieved.
If the list of intervals is empty, return [0, -1].
Task
Implement peak_overlap(intervals: List[List[int]]) -> List[int]
- Input:
intervals— a list of[start, end]pairs where0 <= start < end <= 1440. - Output: a list
[max_count, earliest_minute]wheremax_countis the maximum simultaneous overlap count, andearliest_minuteis the smallest minute index at whichmax_countintervals are active.
Constraints
0 <= len(intervals) <= 2000- Each interval satisfies
0 <= start < end <= 1440 - Multiple intervals may share the same start or end point.
- Minutes are integers in
[0, 1440). - If
intervalsis empty, return[0, -1].
Examples
Example 1
Input: peak_overlap([[60, 120], [90, 150], [100, 200]])
Output: [3, 100]
Example 2
Input: peak_overlap([[0, 1440]])
Output: [1, 0]
Example 3
Input: peak_overlap([])
Output: [0, -1]
Hints
Hint 1
Build a difference array of length 1441: add +1 at each start and -1 at each end, then take a prefix sum to get the active count at every minute.
Hint 2
Scan the prefix-sum array from left to right; the first index where the value equals the global maximum is your answer.
Hint 3
Be careful with the half-open interval semantics: minute `end` is NOT covered, so the -1 goes at index `end`, not `end-1`.
Worked solution
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Key Insight — Difference Array (Sweep Line)
Instead of checking every minute for every interval (O(N × T)), we use a difference array of size 1441 (one slot per minute, 0–1440):
- For each interval
[start, end), incrementdiff[start]by 1 and decrementdiff[end]by 1. - Compute a running prefix sum over
diff. At each indexm, the prefix sum equals exactly the number of intervals active at minutem. - In a single left-to-right scan, track the global maximum and the first minute it is achieved.
Correctness
The half-open convention means minute end is not covered, so the -1 event belongs at index end (not end - 1). The prefix sum faithfully counts active intervals at each minute.
Complexity
- Time: O(N + T) where N = number of intervals and T = 1441 (the time range). Effectively O(N) since T is a fixed constant.
- Space: O(T) = O(1) extra (fixed-size difference array).
Reference Solution
from typing import List
def peak_overlap(intervals: List[List[int]]) -> List[int]:
if not intervals:
return [0, -1]
SIZE = 1441 # minutes 0..1440
diff = [0] * SIZE
for start, end in intervals:
diff[start] += 1
diff[end] -= 1 # half-open: minute `end` is not covered
max_count = 0
current = 0
earliest = -1
for minute in range(SIZE):
current += diff[minute]
if current > max_count:
max_count = current
earliest = minute
return [max_count, earliest]