progress

~/problems / Intervals

Meeting Rooms II

On a phone? Coding is easier on a laptop: email this problem to yourself . Meanwhile: quiz this topic or play this problem's boss fight .

Write min_meeting_rooms(intervals: list[list[int]]) -> int.

Each meeting is [start, end] with start < end, and it occupies a room from start up to, but not including, end. A room freed at time t can be reused by a meeting starting at time t. Return the minimum number of rooms needed to hold every meeting.

Example: [[9, 12], [10, 11], [11, 13], [14, 15]] gives 2. [10, 11] overlaps [9, 12], and [11, 13] reuses the room [10, 11] just freed.

Four meetings packed into two rooms: room 1 holds 9-12 then 14-15, and room 2 holds 10-11 and then reuses the room for 11-13; from 10 to 12 two meetings run at once, so 2 rooms are needed.

More examples: [[1, 5], [5, 9]] gives 1, and [] gives 0.

Constraints: up to 10^5 meetings, 0 <= start < end <= 10^6.

⭐ Bonus: a speed test with 100,000 meetings earns a star in O(n log n). Counting overlaps for each meeting against all the others is O(n²).

Show hint

the answer is the largest number of meetings running at the same moment. Process meetings in start order and track when the rooms currently in use become free again.

Topic: Intervals / sweep line. Sort by start, merge; sweep events for overlaps.

Read the visual guide
0:00
Ctrl ' run · Ctrl ↵ submit
esc