Shubham Sourabhthe room · director's cutplain résumé
Room 04 · The Grind — Intervals

Meeting Rooms II

MEDIUM✓ CHIP-TIMEDLC #253 — FULL STATEMENT

The briefingthe full drill, in my own words

The drill: The same booked day, but now count the minimum number of rooms needed to run every meeting without any two ever sharing a room at the same time.

The same kind of booked day comes in again, but this time one person isn't enough — the goal is to figure out how many rooms would need to exist simultaneously to host every meeting on the list without any two sharing a room.

Meetings can overlap freely; what matters is the peak number of meetings happening at once, anywhere across the whole day, since that peak is exactly how many rooms are required.

A meeting ending at the same instant another begins doesn't need a second room for that instant — the two can pass the same room back to back. The answer is one integer: the minimum room count that covers every simultaneous meeting.

  • up to a few thousand meetings per call
  • start and end times are non-negative integers, end never before start
  • meetings can overlap in any number and pattern
  • a meeting ending exactly when another starts doesn't need an extra room
EX 01
intervals = [[1, 5], [2, 6], [8, 10]]
→ 2
two meetings briefly overlap
EX 02
intervals = [[1, 10], [2, 3], [4, 5], [6, 7]]
→ 2
one wide meeting, several narrow ones, never a triple overlap
EX 03
intervals = [[1, 2], [3, 4], [5, 6]]
→ 1
no overlaps at all

The hintstake only what you need

HINT 1 THE NUDGE

At any instant, the rooms in use equal the number of meetings currently overlapping that instant. Which instant of the whole day is the busiest?

HINT 2 THE STRUCTURE

Track starts and ends as separate events on a timeline: a start adds a room in use, an end frees one. The answer is the peak simultaneous count those events reach.

HINT 3 ONE STEP FROM THE ANSWER

Sort starts and ends separately, then sweep two pointers together: whenever the next start comes before the next end, a new room is needed right now; otherwise a room frees up first. Track the running count and its maximum.

Coach's boardthe pattern, step by step

TWO SORTED TIMELINESPATTERN · SWEEP — STARTS VS ENDSintervals = [[1,5],[2,6],[8,10]]
1
2
8
5
6
10
STEP 1

Starts sorted [1,2,8], ends sorted [5,6,10]. Sweep both together: a start before the next end means a new room is needed right now.

STEP 1 / 8 · ← → WORK TOO

The splitstwo paces, two languages

grind/meeting-rooms-ii.py — RACE PACE
LANG ▸
PACE ▸
class Solution:
    def minMeetingRooms(self, intervals: List[List[int]]) -> int:
        if not intervals:
            return 0
        starts = sorted(iv[0] for iv in intervals)
        ends = sorted(iv[1] for iv in intervals)

        rooms = used = i = j = 0
        n = len(intervals)
        while i < n:
            if starts[i] < ends[j]:
                used += 1
                rooms = max(rooms, used)
                i += 1
            else:
                used -= 1
                j += 1
        return rooms
TIME O(N LOG N)SPACE O(N)PYTHON · RACE PACE · 18 LN

✓ CHIP-TIMEDALL 4 SOLUTIONS RAN GREEN AGAINST SELF-AUTHORED CASES IN CI · JDK 21 · CPYTHON 3.12 · NOTHING PUBLISHES RED