codejump
MediumPythonJavaScriptTypeScript

Intervals — Minimum Meeting Rooms

AlgorithmsIntervalsSortingGreedy

Each meeting is a [start, end] pair. Return the minimum number of rooms
needed so that no two meetings share a room at the same time.

A meeting ending at 10 and one starting at 10 can use the same room:
the interval is half-open, [start, end).

The answer is the largest number of meetings running at the same moment. Sort
the start times and the end times separately and sweep: each start takes a room,
each end that happens at or before the next start frees one. O(n log n).

Examples

solve([[0, 30], [5, 10], [15, 20]])  → 2
solve([[7, 10], [2, 4]])             → 1
solve([[1, 5], [5, 10]])             → 1   // back to back, one room

Constraints

  • 0 <= meetings.length <= 10000
  • 0 <= start < end <= 10^6

Sample tests

Test #1The classic example
Input: [[[0,30],[5,10],[15,20]]]
Output: 2
Test #2No overlap
Input: [[[7,10],[2,4]]]
Output: 1
Test #3Back-to-back meetings share a room
Input: [[[1,5],[5,10]]]
Output: 1