← Whiteboard questions

Meeting Rooms II

Find the minimum number of meeting rooms

DSA, Medium, Intervals

Which structure or algorithm, and what does it cost.

A list meetings holds the meetings of one day, each as [start, end]. Each meeting needs its own room while it runs, from start up to end. A room is free again at the moment a meeting ends, so a meeting that ends at 10 and one that starts at 10 can share a room. Return the fewest rooms that hold all the meetings. The list is not sorted. With no meetings, return 0.

Examples

Input meetings = [[9,14],[8,12],[13,16],[9,10],[11,15]]
Output 3
Why at time 9 three meetings run together, and no time has four.
Input meetings = [[1,5],[2,6],[3,7],[4,8]]
Output 4
Why all four are running at time 4, so each needs its own room.
Input meetings = [[3,4],[4,5],[5,6]]
Output 1
Why each meeting starts as the last one ends, so one room is enough.

Constraints

Your answer must contain

Target Aim for O(n log n) time.

Hints

Try the problem first. Open one hint at a time.

Hint 1 a nudge

At which moment of the day are the most meetings running at once? How could you find that moment without checking every pair of meetings?

Hint 2 the approach

Take the meetings in order of start time. Keep track of the rooms in use and of when each one becomes free.

Hint 3 the key step

Keep the end times of busy rooms in a min-heap. For each meeting, if the earliest end is at or before its start, reuse that room. Otherwise add a room.

Walkthrough

Full walkthrough brute force to best

Brute force

For each meeting, count how many meetings are running at its start time, and return the largest count. The busiest moment always begins at some meeting's start. Each count scans every meeting, so it is O(n^2): about 900 million checks for 30,000 meetings.

Better method

Sort the starts and the ends as two separate lists. Walk the starts in order. Before counting a start, skip every end that is at or before it, and take one room off the count for each. Then add one room. The answer is the largest count. It is O(n log n) too and needs no heap, but it is harder to see why separate lists count rooms correctly. The count after each start is the meetings that have started minus the meetings that have ended, and that is the number running.

Best method

Sort by start time and keep a min-heap of the end times of the rooms in use. For each meeting, look at the earliest end. If it is at or before the meeting's start, that room is free: reuse it by replacing that end with this meeting's end. If not, every room is busy, so open a new room and add the end. The earliest end is the right room to test, because if it is still busy then every other room is too. The heap grows only when all rooms are busy, so at the end its size is the most meetings running at once, which is the fewest rooms that work.

Hand trace

Sort example 1: [8,12], [9,10], [9,14], [11,15], [13,16]. Take [8,12]: the heap is empty, so open a room. Ends: 12. Take [9,10]: the earliest end 12 is above 9, so open a room. Ends: 10, 12. Take [9,14]: the earliest end 10 is above 9, so open a room. Ends: 10, 12, 14. Take [11,15]: the earliest end 10 is at most 11, so reuse it. Ends: 12, 14, 15. Take [13,16]: the earliest end 12 is at most 13, so reuse it. Ends: 14, 15, 16. Three ends remain, so the answer is 3.

Code

import heapq
def min_rooms(meetings):
    ends = []
    for s, e in sorted(meetings):
        if ends and ends[0] <= s:
            heapq.heapreplace(ends, e)
        else:
            heapq.heappush(ends, e)
    return len(ends)

Time and memory

O(n log n) time: the sort, plus at most n heap pushes or replaces at O(log n) each. O(n) memory, because the heap can hold every meeting's end.

Common mistakes

  • Skipping the sort by start. The heap logic assumes each new meeting starts no earlier than the ones before it.
  • Testing end below start instead of end at most start. A room that frees at 10 is free for a meeting that starts at 10.
  • Counting clashing pairs, or the meetings that overlap the first one. The answer is the most meetings at one moment, not the number of clashes.

Related problems

More Intervals problems
Meeting Rooms II: hints, walkthrough and Python solution