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

int peakStaff(std::vector<Slot> slots) {
    
}

Worked examples

CallResult
peakStaff(std::vector<Slot>{Slot{0, 100}, Slot{50, 200}, Slot{60, 80}})3
peakStaff(std::vector<Slot>{Slot{0, 100}, Slot{100, 200}})1
peakStaff(std::vector<Slot>{})0
peakStaff(std::vector<Slot>{Slot{0, 10}, Slot{5, 15}, Slot{10, 20}})2

Hint

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

Reference solution in C++
int peakStaff(std::vector<Slot> slots) {
    std::vector<std::pair<int, int>> events;
    for (const auto& s : slots) { events.push_back({ s.start, 1 }); events.push_back({ s.end, -1 }); }
    std::sort(events.begin(), events.end());
    int cur = 0, best = 0;
    for (auto& e : events) { cur += e.second; best = std::max(best, cur); }
    return best;
}

The same problem in another language

More scheduling problems in C++