Meeting Rooms II

35 min · minMeetingRooms()

One room solves an empty afternoon. A busy calendar may need a whole floor.

You're given intervals, where each [start, end] pair is one meeting. Return the minimum number of rooms needed so that every meeting can happen on schedule.

That number is the largest number of meetings active at the same moment:

  • A meeting uses its room from start up to, but not including, end.
  • When one meeting ends at time 5 and another starts at 5, they can reuse the same room. Touching endpoints aren't simultaneous.
  • Every meeting has positive duration: start < end.

Constraints

  • 1 <= intervals.length <= 100_000
  • Every interval contains exactly two integers: [start, end].
  • 0 <= start < end <= 1_000_000_000

Hints

Watch the room count change

A start time consumes one room. An end time releases one. If you process those changes chronologically, the highest running count is the answer.

Settle ties correctly

When an end and a start share a timestamp, process the end first. The old meeting gives up its room before the new meeting asks for one.

You only need two sorted lists

Sort all starts and all ends separately. Walk the starts from left to right while advancing past every end that is less than or equal to the next start.

Follow-up

Could you also assign a room number to every meeting while still using the minimum?

Visible cases

Examples

Example 1

ready
Input
intervals = [
  [
    0,
    30
  ],
  [
    5,
    10
  ],
  [
    15,
    20
  ]
]
Expected
2
Why
at most two meetings run at once

Example 2

ready
Input
intervals = [
  [
    7,
    10
  ],
  [
    2,
    4
  ]
]
Expected
1
Why
the meetings never overlap, so one room is reusable

Example 3

ready
Input
intervals = [
  [
    1,
    5
  ],
  [
    5,
    10
  ],
  [
    10,
    15
  ]
]
Expected
1
Why
each room is released exactly when the next meeting starts

Interview signal

Asked at

AmazonGoogleUber
Loading editor

Console

ready to run

Ready to run.

3 cases are queued.

Run: visible + custom · Submit: full suite