Skip to lesson

Meeting room allocation finds peak overlapping intervals using min-heaps and chronological sweep-line sweeps.

Meeting Rooms I: Single room attendance check

The first meeting room problem checks whether one person can attend all meetings. Two meetings conflict if their scheduled hours overlap. Brute force compares all pairs in quadratic time.

Step by step

Step 1 of 4

Array: 30.
activeRooms
1
nextMeeting
0
Sorted meetings: [0,30], [5,10], [15,20]. First meeting [0,30] takes room 1.

Example 1 · Single Room Attendance Check

CanAttendMeetingsDemo.javaJava
import java.util.Arrays;
public class CanAttendMeetingsDemo {    public static boolean canAttendMeetings(int[][] intervals) {        Arrays.sort(intervals, (a, b) -> Integer.compare(a[0], b[0]));        for (int i = 1; i < intervals.length; i++) {            if (intervals[i][0] < intervals[i - 1][1]) {                return false;            }        }        return true;    }
    public static void main(String[] args) {        int[][] m1 = {{0, 30}, {5, 10}, {15, 20}};        int[][] m2 = {{7, 10}, {2, 4}};        System.out.println("Can attend m1: " + canAttendMeetings(m1));        System.out.println("Can attend m2: " + canAttendMeetings(m2));    }}
Output
Can attend m1: false
Can attend m2: true

What this code does

  • Sorts meetings by start time to line up adjacent events.
  • Meeting m1 conflicts because [5, 10] starts before [0, 30] finishes.
  • Meeting m2 has no overlaps, so one person can attend both.

Sorting intervals by start time simplifies the conflict check. After sorting, scan through the list comparing each meeting with its predecessor. If any meeting starts before the previous meeting finishes, a conflict occurs. If no overlaps exist, one room suffices and all meetings can be attended. This check runs in log-linear time dominated by sorting. It requires only constant extra space when sorting in place.

Meeting Rooms II with a min-heap

When meetings overlap, multiple rooms must be allocated simultaneously. The goal is finding the minimum rooms needed to host all meetings. This equals the maximum number of concurrent active meetings.

Example 2 · Brute force: count overlaps at every start, O(n^2)

Main.javaJava
public class Main {    public static void main(String[] args) {        int[][] intervals = {{0, 30}, {5, 10}, {15, 20}};        int rooms = 0;
        for (int[] a : intervals) {            int busy = 0;            for (int[] b : intervals) {                 // who is in a meeting when a starts?                if (b[0] <= a[0] && a[0] < b[1]) busy++;            }            rooms = Math.max(rooms, busy);        }        System.out.println("Rooms required: " + rooms);    }}
Output
Rooms required: 2

What this code does

  • The busiest moment always happens at some meeting's start time.
  • For every start, a second loop counts the meetings running then.
  • Sorting plus a heap answers the same question without comparing every pair.

Example 3 · Better: sort and min-heap of end times, O(n log n) time, O(n) space

MinMeetingRoomsHeapDemo.javaJava
import java.util.Arrays;import java.util.PriorityQueue;
public class MinMeetingRoomsHeapDemo {    public static int minMeetingRooms(int[][] intervals) {        if (intervals.length == 0) return 0;        Arrays.sort(intervals, (a, b) -> Integer.compare(a[0], b[0]));
        PriorityQueue<Integer> minHeap = new PriorityQueue<>();        minHeap.offer(intervals[0][1]);
        for (int i = 1; i < intervals.length; i++) {            if (intervals[i][0] >= minHeap.peek()) {                minHeap.poll(); // Room freed, reuse it            }            minHeap.offer(intervals[i][1]);        }        return minHeap.size();    }
    public static void main(String[] args) {        int[][] intervals = {{0, 30}, {5, 10}, {15, 20}};        System.out.println("Rooms required: " + minMeetingRooms(intervals));    }}
Output
Rooms required: 2

What this code does

  • Min-heap tracks the earliest room vacancy time.
  • Meeting [15, 20] reuses the room vacated by [5, 10] at time 10.
  • Final heap size of 2 represents the peak simultaneous room usage.
  • The heap top is the earliest finishing meeting, so one comparison decides whether a room can be reused.

First, sort all meetings by start time. Maintain a min-heap storing the end times of ongoing meetings. The top of the heap holds the meeting that finishes earliest. For each meeting, inspect the earliest finishing room. If the room finishes before the new meeting starts, reuse that room. Remove the old end time and insert the new end time. Otherwise, allocate a new room and add its end time. The final heap size gives the minimum rooms needed.

Fix the error

Fix the heap declaration

Read the error, then pick the line that fixes it.

Round 1 of 2

This heap of end times will not compile. Choose the line that fixes it.

import java.util.*;
public class Main {
public static void main(String[] args) {
PriorityQueue<int> ends = new PriorityQueue<>();
ends.offer(30);
System.out.println(ends.size());
}
}

Error

Main.java:5: error: unexpected type
  required: reference
  found:    int

The sweep line and chronological coordinate sorting

A sweep-line algorithm solves meeting rooms without maintaining a heap. Extract start times and end times into two separate arrays. Sort both arrays independently in ascending order.

Example 4 · Optimal: sweep line over sorted starts and ends, O(n log n) time

SweepLineRoomsDemo.javaJava
import java.util.Arrays;
public class SweepLineRoomsDemo {    public static int minMeetingRooms(int[][] intervals) {        int n = intervals.length;        int[] starts = new int[n];        int[] ends = new int[n];        for (int i = 0; i < n; i++) {            starts[i] = intervals[i][0];            ends[i] = intervals[i][1];        }        Arrays.sort(starts);        Arrays.sort(ends);
        int rooms = 0;        int maxRooms = 0;        int s = 0, e = 0;        while (s < n) {            if (starts[s] < ends[e]) {                rooms++;                maxRooms = Math.max(maxRooms, rooms);                s++;            } else {                rooms--;                e++;            }        }        return maxRooms;    }
    public static void main(String[] args) {        int[][] intervals = {{0, 30}, {5, 10}, {15, 20}};        System.out.println("Sweep-line rooms: " + minMeetingRooms(intervals));    }}
Output
Sweep-line rooms: 2

What this code does

  • Separates and sorts start and end timestamps independently.
  • Advances start pointer when a new meeting starts, incrementing room count.
  • Advances end pointer when a meeting finishes, decrementing room count.
  • Two sorted arrays replace the heap: each start adds a room, each end frees one, and the peak is the answer.

Iterate through the timeline using two pointers. When a start time is smaller than the current end time, a meeting begins. Increment the active room count and advance the start pointer. When a start time is at or after the end time, a meeting finishes. Decrement the active room count and advance the end pointer. Track the peak room count across the entire sweep. This sweep-line idea powers calendar scheduling, car pooling, and capacity monitoring.

Next step

Check your understanding

10 quick questions