AAlgoLoopSpaced repetition for LeetCode
MEDIUMHeap / Priority QueueLeetCode ↗

Meeting Rooms II

The key idea

Sort meetings by start time, then keep a min-heap of the end times of meetings currently using a room. For each new meeting, if the earliest-ending room is already free (its end <= this start) reuse it; otherwise open a new room. The heap size is the number of rooms in use, and its peak is the answer.

Problem

Given an array of meeting time intervals intervals where intervals[i] = [start_i, end_i], return the minimum number of conference rooms required so that no two overlapping meetings share a room.

Two meetings overlap when one starts before the other ends. A meeting that starts exactly when another ends does not overlap, so they may reuse the same room.

Constraints

Examples

Input: intervals = [[0,30],[5,10],[15,20]] Output: 2
Input: intervals = [[7,10],[2,4]] Output: 1

Complexity

Time: O(n log n) Space: O(n)

See the full solution

410310
Step-by-step visualization
Start free →

More Heap / Priority Queue problems