Drill

ProblemsPython › scheduling

Most people on the floor at once

mediumschedulingPython

The register logs every staff entry (start) and exit (end). Find how many were present at the busiest moment.

peak_staff(slots: list<Slot>) → int

Solve it in the editor →

Where you start

def peak_staff(slots: list[Slot]) -> int:
    

Worked examples

CallResult
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

The same problem in another language

More scheduling problems in Python