Given an array of meeting time intervals [[start, end], ...], return the minimum number of conference rooms required.
[[0,30],[5,10],[15,20]]2start ≥ peek() → poll (reuse), else new room. Offer(end)The min-heap tracks when rooms free up. If the earliest-ending meeting ends before the current meeting starts, we reuse that room. Otherwise we need a new room. Heap size = concurrent meetings = rooms needed.
Checking every time point and counting overlaps is slow. We only need to know which room frees up first.
Sort the meetings by start time. Keep a min heap of end times, one per room in use. If the earliest end is not after the new start, reuse that room, otherwise open a new one.
The heap top is the room that frees up first. The heap size is the number of rooms in use.
Run the animation above on your own input, or watch the 1:10 video explanation of Meeting Rooms II with a dry run and Java code.