Meeting Rooms II: the idea to remember.
Find the fewest rooms needed for meetings, reusing a room when its previous meeting ends.
The core insight
Process meetings in start order. Remove all ended meetings from a min heap, add the current end, and track the largest heap size.
Why it works
At each start time, the heap contains exactly the currently active meetings. Their simultaneous count is both a lower bound and a feasible number of rooms.
The mistake to avoid
Free every ended meeting with end <= currentStart. The peak matters, not the final heap size.
Commented Java solution
Use java.util.* imports and a surrounding solution class. Tree snippets assume the standard TreeNode definition; linked lists use ListNode and Clone Graph uses a Node with val and neighbors; Copy Random List uses Node with val, next and random. Read the input assumptions above.
int minMeetingRooms(int[][] meetings) {
Arrays.sort(meetings, (a, b) -> Integer.compare(a[0], b[0]));
PriorityQueue<Integer> active = new PriorityQueue<>(); int best = 0;
for (int[] meeting : meetings) {
while (!active.isEmpty() && active.peek() <= meeting[0]) active.remove();
active.add(meeting[1]); best = Math.max(best, active.size());
}
return best;
}Make the next connection
Independent revision notes with credited Java references. Not affiliated with LeetCode or the source sheets.