Problems › Python › scheduling
Most people on the floor at once
The register logs every staff entry (start) and exit (end). Find how many were present at the busiest moment.
- A person counts from start until end; at the exact end they are clocked out.
- The moments considered are the integers inside each shift.
peak_staff(slots: list<Slot>) → int
Where you start
def peak_staff(slots: list[Slot]) -> int:
Worked examples
| Call | Result |
|---|---|
peak_staff([Slot(start=0, end=100), Slot(start=50, end=200), Slot(start=60, end=80)]) | 3 |
peak_staff([Slot(start=0, end=100), Slot(start=100, end=200)]) | 1 |
peak_staff([]) | 0 |
peak_staff([Slot(start=0, end=10), Slot(start=5, end=15), Slot(start=10, end=20)]) | 2 |
Hint
Build a timeline of arrivals and departures, then sweep. Or sort all events.
Reference solution in Python
def peak_staff(slots: list[Slot]) -> int:
events = []
for s in slots:
events.append((s.start, 1))
events.append((s.end, -1))
events.sort()
cur = best = 0
for _, d in events:
cur += d
best = max(best, cur)
return best