InterviewPrepKit

Home / Coding / Algorithm & Data Structure / Intervals

Meeting Rooms II

medium Original ↗ 00:00

Problem

You are given a list of meetings as half-open time ranges [start, end). Every meeting must be held in some conference room, and a room can host only one meeting at a time. A room frees up the instant its meeting ends, so a meeting starting at time t can reuse a room whose previous meeting ended at t. Return the minimum number of conference rooms needed to schedule all the meetings.

Equivalently: what is the maximum number of meetings that are ever in progress at the same moment?

Examples

  • intervals = [[0,30],[5,10],[15,20]]2[0,30] occupies one room the whole time; [5,10] and [15,20] don’t overlap each other, so they share a second room.
  • intervals = [[7,10],[2,4]]1 — the meetings never coexist.
  • intervals = [[1,4],[2,5],[3,6]]3 — at time 3 all three are running at once.

Constraints

  • 1 <= len(intervals) <= 10^4
  • 0 <= start < end <= 10^6

10^4 meetings makes O(n^2) counting borderline; the expected solutions are O(n log n).

Think about it first

Hint 1 The answer is the peak number of simultaneously running meetings. When can that peak occur — at arbitrary times, or only at moments when some meeting starts?
Hint 2 Process meetings sorted by start. For each new meeting, you need to know one thing: has any currently running meeting already finished? Which running meeting should you check first, and what data structure serves that up cheaply?
Hint 3 Two classic routes: (a) keep a min-heap of end times — pop the earliest end if it's ≤ the new start, then push the new end; the heap's peak size is the answer. (b) Sort all starts and all ends separately and advance two pointers, +1 room per start not matched by an earlier end.

Write your solution, then hit Run tests to check it — or get a mock grade from the AI coach.

The coach remembers this session — revise your code and ask again, and it grades your progress. It gives hints, not the answer.
Report a bug