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
Java 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(List<Slot> slots) {
}
Worked examples
| Call | Result |
|---|---|
peakStaff(Main.<Slot>ls(new Slot(0, 100), new Slot(50, 200), new Slot(60, 80))) | 3 |
peakStaff(Main.<Slot>ls(new Slot(0, 100), new Slot(100, 200))) | 1 |
peakStaff(Main.<Slot>ls()) | 0 |
peakStaff(Main.<Slot>ls(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 Java
int peakStaff(List<Slot> slots) {
List<int[]> events = new ArrayList<>();
for (Slot s : slots) { events.add(new int[]{ s.start, 1 }); events.add(new int[]{ s.end, -1 }); }
events.sort((a, b) -> a[0] != b[0] ? Integer.compare(a[0], b[0]) : Integer.compare(a[1], b[1]));
int cur = 0, best = 0;
for (int[] ev : events) { cur += ev[1]; best = Math.max(best, cur); }
return best;
}