Analyze Schedule Capacity and Overlaps
Greedy & IntervalsAuto-graded · 21 test casesLast updated
Evaluate interval concurrency to determine if single-resource scheduling is possible and calculate peak concurrent demand.
You are given a collection of time reservations for a shared resource. Each reservation is represented as a half-open interval [start, end), where start is the beginning timestamp and end is the completion timestamp. A reservation releasing the resource at time t allows another reservation to claim it starting at time t without collision.
You need to analyze the scheduling requirements and answer two questions:
- Is it possible to fulfill every reservation using a single resource (i.e., no two reservations overlap in time)?
- What is the minimum number of resources needed to serve all reservations simultaneously?
Input / Output
- Input:
a, a list of integer pairs[start, end]representing half-open time intervals wherestart < end. - Output: A list
[can_use_one, min_resources]where:can_use_oneis a boolean (Trueif single resource is sufficient,Falseotherwise).min_resourcesis an integer specifying the peak number of concurrent resources needed.
Constraints
0 <= len(a) <= 10^5- Each interval satisfies
start < end. - Timestamps fit within standard integer limits.
Examples
Example 1
Input: analyze_meetings([[0, 30], [5, 10], [15, 20]])
Output: [False, 2]
Hints
Hint 1
Try sorting the intervals by their starting times to process events chronologically.
Hint 2
How can a min-heap help maintain the active reservations ending earliest?
Hint 3
Remember that intervals are half-open: an event ending at time `t` does not conflict with one starting at time `t`.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
The core challenge is finding the maximum number of overlapping intervals at any instant in time (peak concurrency).
- Sort by Start Time: Sorting the intervals chronologically by
starttime ensures that whenever we process a new interval, all previously considered intervals started no later than the current one. - Track Completion with Min-Heap: Maintain a min-heap containing the end times of all currently active reservations.
- Simulate Time Steps: For each incoming interval
[start, end):- Remove all end times from the top of the heap that are
<= start. Because intervals are half-open[start, end), an interval ending atstartreleases its resource exactly as the new interval begins. - If the heap is non-empty after removing expired intervals, at least one prior reservation is still running. This signifies an overlap.
- Push the current interval's
endtime onto the heap. - Track the maximum heap size encountered across all steps.
- Remove all end times from the top of the heap that are
If any overlap was detected during processing, a single resource is insufficient (can_use_one = False). The minimum number of resources required equals the maximum concurrent active intervals observed.
Complexity
- Time Complexity: $\mathcal{O}(N \log N)$, where $N$ is the number of intervals. Sorting takes $\mathcal{O}(N \log N)$ time, and each interval is pushed and popped from the min-heap at most once ($\mathcal{O}(\log N)$ per heap operation).
- Space Complexity: $\mathcal{O}(N)$ auxiliary space for sorting and storing interval end times in the heap.
import heapq
from typing import List, Union
def analyze_meetings(a: List[List[int]]) -> List[Union[bool, int]]:
if not a:
return [True, 0]
events = sorted(a)
active_ends = []
peak_concurrency = 0
overlap_detected = False
for start, end in events:
while active_ends and active_ends[0] <= start:
heapq.heappop(active_ends)
if active_ends:
overlap_detected = True
heapq.heappush(active_ends, end)
if len(active_ends) > peak_concurrency:
peak_concurrency = len(active_ends)
return [not overlap_detected, peak_concurrency]