LeetEye LeetEye
Medium Intervals ~5 min

Meeting Rooms II

intervals meeting-rooms-ii

Problem

Given an array of meeting time intervals intervals where intervals[i] = [starti, endi], return the minimum number of conference rooms required.

Examples

Example 1
Input: intervals = [[0,30],[5,10],[15,20]]
Output: 2
[0,30] in room 1, [5,10] and [15,20] in room 2.
Example 2
Input: intervals = [[7,10],[2,4]]
Output: 1
Key Insight

Sweep line: track starts and ends as events. Maximum concurrent meetings = minimum rooms needed.

Sweep line: treat starts as +1, ends as -1. Max concurrent count = rooms needed. Or use min-heap of end times, reuse room if meeting ended.

How to Approach This Problem

Pattern Recognition: When you see keywords like meeting rooms ii intervals, think Intervals.

Step-by-Step Reasoning

1 Minimum rooms needed equals:
Answer: Maximum concurrent meetings at any time
At peak concurrency, each meeting needs its own room.
2 We treat starts and ends as:
Answer: Events on a timeline
Start = +1 room needed. End = -1 room needed. Track running count.
3 When a start and end are at same time:
Answer: Process end first
If meeting ends at time t and another starts at t, same room can be reused. End frees room first.
4 Min-heap stores:
Answer: End times of active meetings
Heap tracks when earliest active meeting ends. If new meeting starts after, reuse that room.
5 Add new room when:
Answer: New meeting starts before all current meetings end
If start < heap.min (earliest end), can't reuse any room. Need new one.

Solution

def minMeetingRooms(intervals: List[List[int]]) -> int:
    if not intervals:
        return 0
    
    # Sweep line approach
    events = []
    for start, end in intervals:
        events.append((start, 1))   # +1 room at start
        events.append((end, -1))    # -1 room at end
    
    # Sort: by time, then ends before starts at same time
    events.sort(key=lambda x: (x[0], x[1]))
    
    rooms = 0
    max_rooms = 0
    
    for time, delta in events:
        rooms += delta
        max_rooms = max(max_rooms, rooms)
    
    return max_rooms

Complexity Analysis

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

Master This Pattern

Build intuition with interactive MCQs. Practice Intervals problems in the LeetEye app.

Download LeetEye Free
Practice in LeetEye