Meeting Rooms II

MediumIntervals · Heap · Sweep Line🧩 Intervals & Line SweepLeetCode #253 ↗Updated

Problem

Given meetings intervals[i] = [start, end], each occupying the half-open range [start, end), return the minimum number of conference rooms needed so that no two meetings sharing a room overlap. A room freed at time t can host a meeting starting at t.
Inputintervals = [[0,30],[5,10],[15,20]]Output2
Inputintervals = [[7,10],[2,4]]Output1
Inputintervals = [[1,5],[2,6],[3,7]]Output3

1 ≤ intervals.length ≤ 104  ·  0 ≤ start < end ≤ 106

Try it yourself

Java
class Solution {
    public int minMeetingRooms(int[][] intervals) {
        
    }
}
Your code is saved in this browser.Submit on LeetCode ↗
Key insight
The answer is the peak number of meetings running at the same moment. Only the order of start and end events matters, not which meeting they belong to.
PatternIntervals & Line Sweep

Instinct See [start, end] pairs, meetings, bookings or the word "overlap"? Sort by an endpoint, then sweep left to right carrying one running fact.

Memory hack Sort, then sweep - start time to merge, end time to choose, +1/-1 events to count the crowd.

Open the Intervals & Line Sweep guide →

Approaches

Brute ForceCount Overlaps at Every Start

Time O(n2)Space O(1)

The number of active meetings only rises at a start time, so the peak is reached at some meeting's start. For each start s, count meetings with start ≤ s < end and keep the maximum.

Java
public int minMeetingRooms(int[][] intervals) {
    int best = 0;
    for (int[] m : intervals) {
        int busy = 0; // meetings running at the instant m starts
        for (int[] o : intervals) {
            if (o[0] <= m[0] && m[0] < o[1]) busy++;
        }
        best = Math.max(best, busy);
    }
    return best;
}

No sorting needed and easy to trust, but quadratic.

At a glance

LevelApproachTimeSpace
Brute ForceCount Overlaps at Every StartO(n2)O(1)
BetterSort by Start + Min-Heap of End TimesO(n log n)O(n)
OptimalTwo Sorted Arrays (Chronological Sweep)O(n log n)O(n)
Common pitfalls
  • Using > instead of >= — back-to-back meetings get two rooms.
  • Sorting by end time in the heap approach — it must process meetings by start.
  • Returning the number of overlapping pairs instead of the peak concurrency.
Interview tips
  • State the reduction: rooms needed = maximum concurrent meetings.
  • Show the heap version first; it maps directly to “assign real rooms”.
  • Mention a difference-array sweep when times are small integers.
Pattern
Sweep line / min-heap of ends → count peak overlap of intervals (rooms, platforms, CPUs).
My notes saved in this browser