150NeetCode 150

134.Meeting Rooms II

MediumBlind 75Intervals ยท 5/6

Problem

Given an array of meeting time intervals intervals where intervals[i] = [start_i, end_i], return the minimum number of conference rooms required so that every meeting can take place.

A room freed at time t can immediately host a meeting starting at t: [5,10] and [10,15] can share one room.

Examples

Input:  intervals = [[0,30],[5,10],[15,20]]
Output: 2
Explanation: [0,30] needs its own room; [5,10] and [15,20] can share the second room.
Input:  intervals = [[7,10],[2,4]]
Output: 1
Explanation: The meetings don't overlap.
Input:  intervals = [[1,5],[2,6],[3,7],[5,8]]
Output: 3
Explanation: At time 3, meetings [1,5], [2,6], [3,7] all run. [5,8] reuses the room freed at 5.

Constraints

  • 1 <= intervals.length <= 10^4
  • 0 <= start_i < end_i <= 10^6

๐Ÿ’ก Key Insight

The answer is the maximum number of meetings running at the same moment. Process meetings in start order and track when currently occupied rooms free up: if the earliest-ending room is free by the time the next meeting starts, reuse it; otherwise open a new room. A min-heap of end times does this directly โ€” and since only counts matter, two sorted arrays of starts and ends with two pointers do it with even less overhead.

Brute Force (Count Overlaps at Each Start)

TimeO(nยฒ)SpaceO(1).

Intuition

The number of simultaneous meetings only increases at a start time, so the peak occurs at some meeting's start. For each start s, count how many meetings are active at s (start <= s < end). The largest count is the answer.

Algorithm

  1. For each meeting i, let s = intervals[i][0].
  2. Count meetings j with intervals[j][0] <= s < intervals[j][1].
  3. Track the maximum count and return it.

Visualization

[[0,30],[5,10],[15,20]]
s = 0:  active [0,30]                 -> 1
s = 5:  active [0,30], [5,10]         -> 2
s = 15: active [0,30], [15,20]        -> 2   ([5,10] ended at 10)
max = 2

Code

java
class Solution {
    public int minMeetingRooms(int[][] intervals) {
        int best = 0;
        for (int[] cur : intervals) {
            int s = cur[0];
            int active = 0;
            for (int[] other : intervals) {
                if (other[0] <= s && s < other[1]) { // other is running at time s
                    active++;
                }
            }
            best = Math.max(best, active);
        }
        return best;
    }
}

Complexity

  • Time: O(nยฒ) โ€” n start times, each checked against all n meetings.
  • Space: O(1).

Complexity Comparison

Approach Time Space
Count Overlaps at Each Start O(nยฒ) O(1)
Min-Heap of End Times O(n log n) O(n)
Two Pointers (Starts / Ends) O(n log n) O(n)

Common Pitfalls

  • Using < instead of <= when checking whether a room is free โ€” a meeting ending at 10 frees its room for one starting at 10.
  • Forgetting to sort by start before using the heap โ€” processing out of order gives wrong reuse decisions.
  • Returning the number of polls or the final rooms counter from the wrong branch; with the heap, the answer is its final size.
  • Thinking the two-pointer method loses track of which meeting is in which room โ€” it doesn't need to; only the counts of starts and ends before each moment matter.