DSA AnimatorREVISION VIDEOS
← Back to problems
New

Meeting Rooms II — LeetCode 253 video explanation

Given meeting start and end times, find the minimum number of rooms needed. Meetings that overlap need different rooms.

Loading videos…

DSA Animator › DSA Revision › Intervals › Meeting Rooms II

Key moments in the Meeting Rooms II video

0:04Problem0:11Example0:17Brute force0:23Key idea0:34Dry run0:52Code1:03Takeaway

What you will learn

Remember: the heap top is the room that frees up first. The heap size is the number of rooms in use. The video solves Meeting Rooms II (LeetCode 253, Medium) with the Min Heap of End Times pattern from the Intervals topic, and you can replay every step in the interactive Meeting Rooms II animation.

Meeting Rooms II video transcript

Problem

Given meeting start and end times, find the minimum number of rooms needed. Meetings that overlap need different rooms.

Example

For meetings 0 to 30, 5 to 10 and 15 to 20, the answer is 2 rooms.

Brute force

Checking every time point and counting overlaps is slow. We only need to know which room frees up first.

Key idea

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.

Dry run

0 to 30 opens room 1, and the heap holds 30. 5 to 10 starts before 30, so open room 2, and the heap holds 10 and 30. 15 to 20 starts after 10, so it reuses that room, and the heap holds 20 and 30. The heap size, 2, is the number of rooms.

Code

In code, sort by start. For each meeting, if the heap top is at most the start, poll it. Then offer the meeting end, and return the heap size. It's O of n log n time and O of n space.

Takeaway

Remember: the heap top is the room that frees up first. The heap size is the number of rooms in use.

More Intervals revision videos

← All 185 DSA revision videos