Drill

ProblemsC# › scheduling

Most people on the floor at once

mediumschedulingIntervalsSortingC#

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

PeakStaff(slots: list<Slot>) → int

C# needs a compiler and Drill does not host one yet, so this page is the reference rather than an exercise: the problem, worked examples, and the solution in full. To type it out, the same problem runs in Python.

Solve it in Python →

Where you start

public int PeakStaff(List<Slot> slots) {
    
}

Worked examples

CallResult
PeakStaff(new List<Slot> { new Slot(0, 100), new Slot(50, 200), new Slot(60, 80) })3
PeakStaff(new List<Slot> { new Slot(0, 100), new Slot(100, 200) })1
PeakStaff(new List<Slot> { })0
PeakStaff(new List<Slot> { new Slot(0, 10), new Slot(5, 15), new Slot(10, 20) })2

Hint

Build a timeline of arrivals and departures, then sweep. Or sort all events.

Reference solution in C#
public int PeakStaff(List<Slot> slots) {
    var events = new List<(int Time, int Delta)>();
    foreach (var s in slots) { events.Add((s.Start, 1)); events.Add((s.End, -1)); }
    events = events.OrderBy(e => e.Time).ThenBy(e => e.Delta).ToList();
    int cur = 0, best = 0;
    foreach (var ev in events) { cur += ev.Delta; best = Math.Max(best, cur); }
    return best;
}

The same problem in another language

More scheduling problems in C#