mediumGreedySortingHeapHeap / Priority Queue

Meeting Rooms II

mediumTime: O(n log n)Space: O(n)

Recognize the pattern

minimum meeting rooms neededpeak overlapping meetingsstart and end events

Brute force idea

If you approach Meeting Rooms II in the most literal way possible, you get this: For each meeting, count how many others overlap with it. Checks every pair. It is a fair place to begin because it matches the surface of the question, yet it does not capture the deeper structure that makes the problem simpler.

Better approach

The real unlock in Meeting Rooms II comes when you notice this: Separate start and end times, sort both. Two pointers: if next event is a start, increment rooms; if end, decrement. Track the peak. Or use a min-heap of end times. Instead of recomputing the world every time, you preserve just enough context to let the next decision become obvious.

Key invariant

The compass for Meeting Rooms II is this: The minimum rooms needed equals the maximum number of simultaneously active meetings. Sorting events chronologically and tracking active count reveals this peak. As long as that statement keeps holding, you can trust the steps built on top of it.

Watch out for

One easy way to drift off course in Meeting Rooms II is this: Using a max-heap instead of min-heap — the min-heap tracks the earliest ending meeting. If a new meeting starts after it ends, reuse that room (pop and push). The fix is usually to return to the meaning of each move, not just the steps themselves.

Heap / Priority Queue Pattern