1. Sorted starts
Separate and sort start times to process events in order.
const starts = intervals.map((x) => x[0]).sort((a, b) => a - b)Typorithm · medium
Step 1 of 8 · intro card
Intervals
Split into sorted start and end times; sweep both, adding a room on each start before the next end, tracking the peak.
The minimum rooms equals the maximum number of meetings happening at once.
Split intervals into sorted start and end times and sweep them like a merge.
Each start before the next end opens a room; otherwise a meeting has ended and a room frees up.
Answer = peak concurrent meetings.
Sort starts and ends separately.
Start before next end → new room.
function minMeetingRooms(intervals) {
const starts = intervals.map((x) => x[0]).sort((a, b) => a - b);
const ends = intervals.map((x) => x[1]).sort((a, b) => a - b);
let rooms = 0;
let maxRooms = 0;
let s = 0;
let e = 0;
while (s < starts.length) {
if (starts[s] < ends[e]) {
rooms += 1;
maxRooms = Math.max(maxRooms, rooms);
s += 1;
} else {
rooms -= 1;
e += 1;
}
}
return maxRooms;
}Connect each part of the code to the concept before you type.
Separate and sort start times to process events in order.
const starts = intervals.map((x) => x[0]).sort((a, b) => a - b)Sort end times to know when rooms free up.
const ends = intervals.map((x) => x[1]).sort((a, b) => a - b)A meeting starting before the next end needs a new room.
if (starts[s] < ends[e]) {The answer is the maximum simultaneous meetings.
maxRooms = Math.max(maxRooms, rooms)Read the concept map and follow the code breakdown.
Type the implementation, repair mistakes and work through every recall exercise.
Use the final check without hints; revisit weak concepts in short practice sessions.