Home
DSA Visualizer
Advanced Topics
Meeting Rooms II
Meeting Rooms II
Meeting Rooms II asks for the minimum number of conference rooms needed to schedule all meetings, solved with the sweep line algorithm.
READY
Schedule
A
B
C
D
0
30
Moments · in time order
A
B
B
D
D
C
C
A
Rooms
0
Peak
0
Needed
2
4 meetings, and the question is how many rooms the day needs. Comparing every pair to see which overlap would answer it, and would take 6 comparisons to do it — but no room is ever needed at a moment when nothing starts or ends, so only those 8 moments have to be looked at.
Next
The 8 moments in time order, each one either a meeting starting or a meeting ending. Where a start and an end share a time the end is read first, because the room is free the instant the meeting is over.
Schedule
Tie-break
Intuition
The Core Idea: Instead of checking every minute of the day to see how many meetings are happening, we only care about the moments when the number of active meetings changes.
These moments are the Start and End times of the meetings.
Imagine a vertical line sweeping across the timeline. Every time it hits a Start, we need a room (+1). Every time it hits an End, a room becomes free (-1).
Concept
Sweep Line (Chronological Ordering):
- Events: Deconstruct every interval
[start, end]into two separate events:(start, +1)and(end, -1). - Sorting: Sort all events by time. If times are equal, process the End event first (to free up the room before re-allocating it).
- Scanning: Iterate through the sorted events, maintaining a running sum of active meetings. The maximum value of this sum is the answer.
How it Works
The visualizer effectively sorts the start and end times.
- Timeline: Meetings are placed on a timeline.
- Scan: A vertical line moves from left to right.
- Counter: As the line passes a 'Start', the 'Active Rooms' counter increases. As it passes an 'End', it decreases.
- Result: The peak value of the counter is recorded as the minimum rooms required.
Step-by-Step Breakdown
1. Separate: Create an array of start times and an array of end times.
2. Sort: Sort both arrays.
3. Two Pointers: Use s_ptr for starts and e_ptr for ends.
4. Iterate:
- If start[s_ptr] < end[e_ptr]: A meeting starts. rooms++. s_ptr++.
- Else: A meeting ends. rooms-- (or reuse). e_ptr++.
5. Track Max: Keep track of the maximum rooms needed at any point.
When to Use
- Finding maximum concurrency/overlap in intervals.
- Resource allocation problems (rooms, platforms, bandwidth).
- "Merge Intervals" variations.
When NOT to Use
- When you need to select a subset of non-overlapping intervals (Activity Selection / Greedy).
- When intervals have weights/values and you want to maximize value (Weighted Interval Scheduling / DP).
How to Identify
"Minimum resources required", "Maximum simultaneous events", "Find the point of maximum overlap".
Applying for software engineering roles? Free, no sign-up:
