Meeting Rooms II

MediumIntervals

You get a list of meetings, each written as [start, end], in no particular order. Return the fewest rooms needed to hold all of them, when two meetings that overlap cannot share a room.

A room is free again the moment its meeting ends, so a meeting from 4 to 9 and one from 9 to 12 can use the same room.

Examples

Example 1
Input:  intervals = [[1, 5], [2, 6], [7, 9]]
Output: 2

1 to 5 and 2 to 6 run at once. By 7 both are over, so the last one reuses a room.

Example 2
Input:  intervals = [[4, 9], [9, 12]]
Output: 1

The second meeting starts as the first one ends.

Constraints

  • 0 ≤ intervals.length ≤ 10⁴
  • 0 ≤ start < end ≤ 10⁶

Stuck?

Hint 1

The answer is the largest number of meetings going on at the same moment.

Hint 2

Sort the start times and the end times separately and walk both with two pointers: a start before the next end needs another room, otherwise a room frees up. A min-heap of end times works too.

Still stuck? Read the solution

JavaScript
Loading the editor

Run checks your code against the 2 examples. Submit checks all 9 tests, most of them hidden.

Ctrl + Enter runs, and adding Shift submits. On a Mac, use ⌘.

blur