Meeting Rooms II

Medium
126.

Meeting Rooms II

Meeting Rooms II is a medium coding problem on arrays, sorting and heap, asked in Atlassian, Snapchat, Yahoo and Yelp interviews.

Given an array of meeting time intervals ([start, end)), return the minimum number of conference rooms required to host them all. A room freed at time t can host a meeting starting at t.

Example 1

Input
intervals = [[0,30],[5,10],[15,20]]
Output
2

Example 2

Input
intervals = [[7,10],[2,4]]
Output
1

Constraints

  • 1 <= intervals.length <= 20
  • 0 <= start < end <= 100
Write your solution here. Running it against the tests needs a free account — what you type comes with you.Sign in to run
/**
 * @param {number[][]} intervals
 * @return {number}
 */
var minMeetingRooms = function(intervals) {
    // Write your code here
    
};

Sign in to run your own test cases

Sign in

Meeting Rooms II — Solution in Python, Java & C++

Meeting Rooms II solution explained: step-by-step approach, complexity and code in Python, Java, C++ and JavaScript. A medium problem on arrays and sorting.

Meeting Rooms II is a medium coding problem on arrays, sorting and heap, asked in Atlassian, Snapchat, Yahoo and Yelp interviews.

Problem statement

Given an array of meeting time intervals ([start, end)), return the minimum number of conference rooms required to host them all. A room freed at time t can host a meeting starting at t.

Example 1

Input: intervals = [[0,30],[5,10],[15,20]]
Output: 2

Example 2

Input: intervals = [[7,10],[2,4]]
Output: 1

Constraints

  • 1 <= intervals.length <= 20
  • 0 <= start < end <= 100

Hints for Meeting Rooms II

  1. Hint 1

    Think of +1 events at starts and -1 events at ends on a timeline.

  2. Hint 2

    Sort events (ends before starts at the same time); the running sum's peak is the answer.

How to solve Meeting Rooms II

Forget which meeting goes in which room — you only need the peak concurrency. Turn each meeting into two timeline events, +1 at its start and -1 at its end, sort them, and sweep. The running total is how many meetings are live at that instant, and its maximum is the number of rooms.

Approach

  1. Collect the start times and the end times into two sorted lists (or one list of signed events).
  2. Sweep with two pointers: whichever of the next start or next end comes first is processed next.
  3. A start increments the live count; an end decrements it.
  4. Track the largest live count seen.
  5. That maximum is the answer.

Why it works

A room is needed for each meeting running at the same moment, so the answer is the maximum number of intervals covering any single point — and that maximum can only change at an endpoint, which is why sampling the events suffices. It is achievable too: whenever a meeting ends, its room is genuinely free for the next one, so the peak is not just a lower bound but the exact requirement.

Complexity

  • Time — O(n log n)
  • Space — O(n)

Pitfalls

  • Process ends before starts at the same timestamp — a room freed at time t can host a meeting starting at t. Getting this backwards over-counts by one.
  • You are matching sorted starts against sorted ends, not against their original pairs; the events are deliberately decoupled.
  • Track the running maximum, not the final count — the count ends at zero.

Reference solution

Python

from typing import List

def minMeetingRooms(intervals: List[List[int]]) -> int:
    events = []
    for s, e in intervals:
        events.append((s, 1))
        events.append((e, -1))
    events.sort()
    cur = 0
    best = 0
    for _, d in events:
        cur += d
        best = max(best, cur)
    return best

Java

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 live = 0, best = 0, i = 0, j = 0;
    while (i < n) {
        if (starts[i] < ends[j]) {
            live++;
            if (live > best) best = live;
            i++;
        } else {
            live--;
            j++;
        }
    }
    return best;
}

C++

int minMeetingRooms(vector<vector<int>>& intervals) {
    int n = (int) intervals.size();
    vector<int> starts, ends;
    for (const auto& iv : intervals) {
        starts.push_back(iv[0]);
        ends.push_back(iv[1]);
    }
    sort(starts.begin(), starts.end());
    sort(ends.begin(), ends.end());
    int live = 0, best = 0, i = 0, j = 0;
    while (i < n) {
        if (starts[i] < ends[j]) {
            live++;
            if (live > best) best = live;
            i++;
        } else {
            live--;
            j++;
        }
    }
    return best;
}

JavaScript

var minMeetingRooms = function(intervals) {
    const events = [];
    for (const iv of intervals) {
        events.push([iv[0], 1], [iv[1], -1]);
    }
    events.sort(function(a, b) { return a[0] - b[0] || a[1] - b[1]; });
    let cur = 0, best = 0;
    for (const e of events) {
        cur += e[1];
        best = Math.max(best, cur);
    }
    return best;
};

Also on the editorial tab: C, C#, Go, Kotlin, PHP, Ruby, Rust, Swift, TypeScript.

All 1199 arrays problems · the whole catalogue

Learn the technique: Arrays · Sorting Algorithms