Field manual
Sweep line (event counting)
O(n log n)Turn each interval into a start event (+1) and an end event (-1), sort all events by position, then sweep through adding deltas to a running counter to find the busiest moment.
Signals
maximum number of overlapping meetings/intervals at onceminimum rooms/resources neededbusiest moment in timehow many intervals cover a given pointevents happening at the same time
Template
function maxOverlap(intervals) {
const events = [];
for (const [start, end] of intervals) {
events.push([start, 1]);
events.push([end, -1]);
}
events.sort((a, b) => a[0] - b[0] || a[1] - b[1]);
let active = 0;
let best = 0;
for (const [, delta] of events) {
active += delta;
best = Math.max(best, active);
}
return best;
}Looks similar, isn't
- Intervals: merge & insert: Merge outputs combined, non-overlapping ranges as the final answer. A sweep line does not combine ranges; it counts how many are simultaneously active, which is what you need for 'minimum meeting rooms', not a merged schedule.
n up to 1e5 intervals -> O(n log n): 2n events sorted by position, then one linear sweep.
Learn this pattern