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.
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.
Where you start
int peakStaff(std::vector<Slot> slots) {
}
Worked examples
| Call | Result |
|---|---|
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;
}