KatInterview

Peak Crew Size for Scheduled Shifts

Greedy & IntervalsAuto-graded · 16 test cases
MediumAuto-gradedGreedy & Intervalssweep-lineintervalsgreedy

Last 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):

  • start and end are strings in 24-hour "HH:MM" format.
  • needed is 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.
  • needed is 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.