Peak Crew Size for Scheduled Shifts
Greedy & IntervalsAuto-graded · 16 test casesLast updated
Compute the smallest crew that covers every scheduled task by finding the peak simultaneous demand, treating each task as half-open and parsing HH:MM times.
Staffing a Day of Tasks
A facility has a list of tasks to run during a single day. Each task has a start time, an end time, and a number of crew members it needs for its whole duration. A crew member can work on only one task at any moment, and anyone is free to move to a different task the instant their current one ends. Crew members are interchangeable.
Return the smallest total number of crew members that must be on hand so every task can be fully staffed.
Input
tasks is a list of triples (start, end, needed):
startandendare strings in 24-hour"HH:MM"format.neededis an integer, the number of crew members the task requires throughout its run.
Time semantics
A task occupies the half-open interval [start, end), measured in minutes since midnight. For example, a task from 10:00 to 11:00 occupies minutes [600, 660). Because the end is exclusive, a task that ends at a given minute and another that starts at that same minute do not overlap, so the same people can handle both.
Output
Return a single integer: the minimum number of crew members needed.
Constraints
1 <= len(tasks) <= 1000- Times are valid
"HH:MM"strings within a single day (no wraparound past midnight). - For every task,
start < end. neededis an integer count of crew members for that task.
Examples
Example 1
Input: min_crew_needed([['09:00', '10:00', 2], ['09:30', '10:30', 1]])
Output: 3
Example 2
Input: min_crew_needed([['08:00', '09:00', 1], ['09:00', '10:00', 1]])
Output: 1
Hints
Hint 1
Because crew members are interchangeable and can be reassigned instantly, the answer equals the largest total demand at any single moment.
Hint 2
Convert each time to minutes since midnight, then turn every task into two events: demand goes up at its start and down at its end.
Hint 3
Sort the events by time. When an end and a start share a minute, handle the end first, since the interval is half-open and those people are already free.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
The minimum crew size equals the peak total demand at any single moment. Crew members are interchangeable and become free the instant a task ends, so you never need more people than the maximum number simultaneously busy, and you cannot manage with fewer.
To find that peak, use a sweep line:
- Convert each
"HH:MM"string to minutes since midnight (HH * 60 + MM). - For each task, create a start event that adds
neededand an end event that subtractsneeded. - Sort events by time. For equal times, process end events before start events.
- Walk through the events keeping a running total, and record its maximum after each start event.
The tie-break encodes the half-open interval rule: a task ending at minute t and another starting at t do not overlap, so the freed people must be released before the new task claims anyone.
Why it is correct
- Lower bound: at the moment of peak demand
P, the tasks running needPdistinct people, since each person handles one task at a time. So at leastPare required. - Upper bound: keep a pool of idle people. When a task ends, return its people to the pool. When a task starts, take people from the pool and hire new ones only if the pool is short. Hiring happens only when everyone already hired is busy, so the total hired never exceeds the current busy count, hence never exceeds
P.
So the answer is exactly P. The maximum only needs checking after start events, because the running total increases only at starts.
Complexity
- Time: O(n log n) for sorting the 2n events.
- Space: O(n) for the events.
Reference solution
from typing import List, Tuple
def min_crew_needed(tasks: List[Tuple[str, str, int]]) -> int:
def to_minutes(value: str) -> int:
hour, minute = map(int, value.split(':'))
return hour * 60 + minute
events = []
for start, end, needed in tasks:
events.append((to_minutes(start), 1, needed)) # 1 = start
events.append((to_minutes(end), 0, needed)) # 0 = end
# At equal times, end events (0) come before start events (1).
events.sort(key=lambda e: (e[0], e[1]))
active = 0
best = 0
for _, kind, needed in events:
if kind == 0:
active -= needed
else:
active += needed
if active > best:
best = active
return best