LeetCode Β· #253

Meeting Rooms II Solution

Given an array of meeting intervals, return the minimum number of conference rooms required.

01
Section One Β· Problem

Problem Statement

🏷️

Difficulty

Medium

πŸ”—

LeetCode

Problem #253 (Premium)

πŸ—οΈ

Pattern

Intervals β€” min-heap / sweep line

Given intervals where intervals[i] = [startα΅’, endα΅’], return the minimum number of conference rooms required to hold all meetings.

Example
Input: intervals = [[0,30],[5,10],[15,20]] Output: 2 // [0,30] conflicts with [5,10] β†’ need 2 rooms. [15,20] fits in room freed by [5,10].
Constraints
β€’ 1 ≀ intervals.length ≀ 10⁴
02
Section Two Β· Approach 1

Sweep Line β€” O(n log n)

Separate all start times (+1 event) and end times (-1 event). Sort events. Sweep through, tracking running count. The peak count is the answer.

Why it works

Each start adds a concurrent meeting, each end removes one. The maximum concurrency = rooms needed.

Java β€” Sweep line
import java.util.*; class Solution { public int minMeetingRooms(int[][] intervals) { int[] starts = new int[intervals.length]; int[] ends = new int[intervals.length]; for (int i = 0; i < intervals.length; i++) { starts[i] = intervals[i][0]; ends[i] = intervals[i][1]; } Arrays.sort(starts); Arrays.sort(ends); int rooms = 0, e = 0; for (int s : starts) { if (s < ends[e]) rooms++; // new room needed else e++; // reuse freed room } return rooms; } }
Python β€” Sweep line
class Solution: def minMeetingRooms(self, intervals: list[list[int]]) -> int: starts = sorted(s for s, e in intervals) ends = sorted(e for s, e in intervals) rooms = ep = 0 for s in starts: if s < ends[ep]: rooms += 1 else: ep += 1 return rooms
03
Section Three Β· Approach 2

Min-Heap β€” O(n log n)

Sort by start. Use a min-heap of end times (earliest room to free). For each meeting: if its start β‰₯ heap's min β†’ reuse that room (poll + push new end). Else push new end (new room).

πŸ’‘ Mental model: Imagine you're a receptionist. You have a list of meetings sorted by start time and a priority board showing when each room frees up (min-heap). For each meeting: peek at the board β€” if the earliest-freeing room is free by now, assign that room (update its end time). Otherwise, open a new room. The heap size = rooms in use.
Algorithm
  • Sort intervals by start.
  • Min-heap of end times. Push first meeting's end.
  • For each subsequent meeting: if start β‰₯ heap.peek() β†’ heap.poll() (reuse room).
  • Push current meeting's end. Heap size = rooms in use.
  • Return max heap size (= heap.size() at end).
🎯 When to recognize this pattern: "How many resources needed concurrently?" (rooms, servers, threads). Two classic approaches: sweep line (sort events) and min-heap (track active resource end times). Both are O(n log n). Sweep line is simpler; min-heap is more intuitive for "room allocation" reasoning.
04
Section Four Β· Trace

Visual Walkthrough

Trace for [[0,30],[5,10],[15,20]] using sweep line.

Meeting Rooms II β€” Sweep Line
starts=[0,5,15], ends=[10,20,30]. Two pointers. s=0: 0 < ends[0]=10 β†’ rooms=1. s=5: 5 < ends[0]=10 β†’ rooms=2. s=15: 15 β‰₯ ends[0]=10 β†’ reuse. ep=1. rooms stays 2. return 2 βœ“
05
Section Five Β· Implementation

Code β€” Java & Python (Min-Heap)

Java β€” Min-heap
import java.util.*; class Solution { public int minMeetingRooms(int[][] intervals) { Arrays.sort(intervals, (a, b) -> a[0] - b[0]); PriorityQueue<Integer> heap = new PriorityQueue<>(); for (int[] iv : intervals) { if (!heap.isEmpty() && iv[0] >= heap.peek()) heap.poll(); // reuse room heap.offer(iv[1]); // occupy room until end } return heap.size(); } }
Python β€” Min-heap
import heapq class Solution: def minMeetingRooms(self, intervals: list[list[int]]) -> int: intervals.sort() heap = [] for s, e in intervals: if heap and s >= heap[0]: heapq.heapreplace(heap, e) # reuse room else: heapq.heappush(heap, e) # new room return len(heap)
06
Section Six Β· Analysis

Complexity Analysis

ApproachTimeSpaceTrade-off
Sweep lineO(n log n)O(n)Two sorted arrays; two pointers
Min-heapO(n log n)O(n)Heap tracks active rooms; more intuitive for allocation
07
Section Seven Β· Edge Cases

Edge Cases & Pitfalls

CaseInputWhy It Matters
One meeting[[1,5]]1 room.
No overlap[[1,2],[3,4]]1 room.
All overlap[[1,5],[2,6],[3,7]]3 rooms (all concurrent at time 3).
Touching[[1,5],[5,10]]start=5 β‰₯ end=5 β†’ reuse. 1 room.
⚠ Common Mistake: In the sweep-line approach, using s ≀ ends[e] instead of s < ends[e]. If a meeting starts exactly when another ends, they don't overlap β€” the room is free. Use strict less-than to require a new room only when the meeting truly overlaps.

← Back to Interval problems