Meeting Rooms III

Hard Intervals Line Sweep

Meeting Rooms III is a hard intervals problem solved with the line sweep pattern. The best approach, optimal (two heaps), runs in O(m log m + m log n) time and O(n) space. Below are 2 approaches in Java, Python, C++ and JavaScript, from simulation with a scan over the rooms up.

Problem

There are n rooms numbered 0 to n - 1 and a list of meetings, each given as a half-open interval [start, end) with a distinct start time. Meetings are handled in order of start time: a meeting takes the free room with the smallest number. If every room is busy, it waits until a room frees up (the one that frees up first, lowest number on ties) and then runs for its original length.

Return the number of the room that hosted the most meetings. If several rooms tie, return the smallest number.

Examples

Example 1

Input
n = 2, meetings = [[2, 5], [0, 6], [4, 8], [1, 3]]
Output
0
Why
[0,6) takes room 0 and [1,3) takes room 1. [2,5) waits for room 1 and runs [3,6). [4,8) waits; both rooms free up at 6, so it takes room 0 and runs [6,10). Each room holds 2 meetings, so the lower number wins.

Example 2

Input
n = 3, meetings = [[0, 10], [1, 3], [2, 6], [4, 5], [7, 12], [8, 9]]
Output
1
Why
Room 1 hosts [1,3), [4,5) and [7,12), which is more than any other room.

Constraints

  • 1 <= n <= 100
  • 1 <= meetings.length <= 10^5
  • 0 <= start_i < end_i <= 5 * 10^5
  • All start times are different.

Solve it here

Write your solution in Java, Python, C++ or JavaScript and run it against the examples, right in the page. Submit runs the hidden tests too and, with a free account, records the solve in your revision log.

00:00
Testcase

Run checks the testcases above (2 examples, or your own). Submit also runs 13 hidden tests and saves the solve to your log. Signed in, every Run and Submit is saved under My submissions; your draft stays in this browser.

Animated walkthrough

A narrated, step-by-step animation that builds the solution from the idea up. Press play, or step through it at your own pace.

Concepts first, then the problem and every approach, step by step.

Space to play or pause · ← → to jump a step · click or drag the bar to seek

Solutions

Try it yourself first. Then compare: each approach lists its idea, the steps, its time and space, and the code in Java, Python, C++ and JavaScript.

ApproachTimeSpace
Simulation with a scan over the roomsO(m log m + m * n)O(n)
Optimal (two heaps)O(m log m + m log n)O(n)

1Simulation with a scan over the rooms

TimeO(m log m + m * n)
SpaceO(n)

Sort the meetings by start time and keep, for each room, the time it becomes free. For each meeting, scan the rooms: the first one that is already free takes it. If none is free, the room with the earliest free time (lowest number on ties) takes it, and the meeting is shifted to start then.

  1. Sort meetings by start. freeAt[i] = 0 and count[i] = 0 for every room.
  2. For each meeting, look for the lowest i with freeAt[i] <= start; while scanning, also track the room with the smallest freeAt.
  3. If a free room exists, set freeAt[room] = end. Otherwise use the earliest room and set freeAt[room] += end - start.
  4. count[room]++. At the end return the lowest index with the largest count.
class Solution {
    public int mostBooked(int n, int[][] meetings) {
        Arrays.sort(meetings, (a, b) -> Integer.compare(a[0], b[0]));
        long[] freeAt = new long[n];
        int[] count = new int[n];
        for (int[] m : meetings) {
            int room = -1, earliest = 0;
            for (int i = 0; i < n; i++) {
                if (freeAt[i] <= m[0]) { room = i; break; }
                if (freeAt[i] < freeAt[earliest]) earliest = i;
            }
            if (room >= 0) {
                freeAt[room] = m[1];
            } else {
                room = earliest;
                freeAt[room] += m[1] - m[0];
            }
            count[room]++;
        }
        int best = 0;
        for (int i = 1; i < n; i++) if (count[i] > count[best]) best = i;
        return best;
    }
}

2Optimal (two heaps)

TimeO(m log m + m log n)
SpaceO(n)

Replace the scan with two priority queues. One holds the free room numbers, so the smallest is on top. The other holds busy rooms as (free time, room), so the room that frees up first is on top, with ties broken by number. Each meeting first releases every room that is free by its start, then takes the smallest free room or, if there is none, waits for the top busy room.

  1. Sort meetings by start. Put rooms 0..n-1 into the free heap.
  2. For each meeting: while busy.top.time <= start, move that room to the free heap.
  3. If the free heap is not empty, pop a room and push (end, room) into busy.
  4. Otherwise pop (t, room) from busy and push (t + (end - start), room): the meeting is delayed to t.
  5. count[room]++. Return the lowest index with the largest count.
class Solution {
    public int mostBooked(int n, int[][] meetings) {
        Arrays.sort(meetings, (a, b) -> Integer.compare(a[0], b[0]));
        PriorityQueue<Integer> free = new PriorityQueue<>();
        for (int i = 0; i < n; i++) free.offer(i);
        PriorityQueue<long[]> busy = new PriorityQueue<>(
            (a, b) -> a[0] != b[0] ? Long.compare(a[0], b[0]) : Long.compare(a[1], b[1]));
        int[] count = new int[n];
        for (int[] m : meetings) {
            long start = m[0], duration = m[1] - m[0];
            while (!busy.isEmpty() && busy.peek()[0] <= start) free.offer((int) busy.poll()[1]);
            int room;
            if (!free.isEmpty()) {
                room = free.poll();
                busy.offer(new long[] { start + duration, room });
            } else {
                long[] top = busy.poll();
                room = (int) top[1];
                busy.offer(new long[] { top[0] + duration, room });
            }
            count[room]++;
        }
        int best = 0;
        for (int i = 1; i < n; i++) if (count[i] > count[best]) best = i;
        return best;
    }
}

Edge cases to test

  • A single room: every meeting goes there
  • More rooms than meetings: the lowest-numbered rooms are used first
  • Several rooms become free at the same time: the delayed meeting takes the lowest number
  • A room freed exactly at a meeting's start time counts as free (intervals are half-open)
  • Long chains of delays push end times past the int range of the input, so use long

Hints

Hint 1

Process the meetings in order of start time; the start times are unique, so the order is fixed.

Hint 2

Keep the free rooms in a min-heap by room number, and the busy rooms in a min-heap by (free time, room number).

Hint 3

Before placing a meeting, move every busy room whose free time is at most the start into the free heap.

Hint 4

If no room is free, pop the earliest busy room; the meeting starts when it frees up and keeps its original length.

FAQ

What is the best time complexity for Meeting Rooms III?

Optimal (two heaps) runs in O(m log m + m log n) time and O(n) extra space.

Which pattern does Meeting Rooms III use?

It is a intervals problem that uses the line sweep pattern. Other problems with the same pattern: Meeting Rooms II, Car Pooling, Employee Free Time.

Is there a brute force solution for Meeting Rooms III?

Yes. Simulation with a scan over the rooms takes O(m log m + m * n) time and O(n) space. Sort the meetings by start time and keep, for each room, the time it becomes free.

Which edge cases should I test for Meeting Rooms III?

A single room: every meeting goes there; More rooms than meetings: the lowest-numbered rooms are used first; Several rooms become free at the same time: the delayed meeting takes the lowest number; A room freed exactly at a meeting's start time counts as free (intervals are half-open); Long chains of delays push end times past the int range of the input, so use long.