Typorithm · medium

Meeting Rooms II

meeting-rooms-iiintervalssweep-linesplit-endpointsconcurrency
Saved on this device
Full lesson · 8 learning steps Open any exercise for focused practice
1

Step 1 of 8 · intro card

Learn Meeting Rooms II

0%

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.

01

Answer = peak concurrent meetings.

02

Sort starts and ends separately.

03

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;
}

Read the implementation

Connect each part of the code to the concept before you type.

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)

2. Sorted ends

Sort end times to know when rooms free up.

const ends = intervals.map((x) => x[1]).sort((a, b) => a - b)

3. Meeting begins

A meeting starting before the next end needs a new room.

if (starts[s] < ends[e]) {

4. Peak concurrency

The answer is the maximum simultaneous meetings.

maxRooms = Math.max(maxRooms, rooms)

A complete learning loop

01

Understand

Read the concept map and follow the code breakdown.

02

Reconstruct

Type the implementation, repair mistakes and work through every recall exercise.

03

Verify

Use the final check without hints; revisit weak concepts in short practice sessions.

Meeting Rooms II: Lesson & Practice · Typorithm | ApplyDjinn