228/670

228. Meeting Rooms II

Medium

You are given intervals, a list of meetings where intervals[i] = [start_i, end_i] means the i-th meeting occupies a room from start_i up to (but not including) end_i. Every meeting needs a room to itself for its whole duration. Return the minimum number of rooms required to host the entire schedule.

A room frees up the instant its meeting ends, so a meeting may start at exactly the moment another one finishes in the same room.

[[0, 30], [5, 10], [15, 20]] packed into two rooms
Room 1[0, 30]Room 2[5, 10][15, 20]t = 7 → 2 meetings running0102030

Example 1:

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

Output: 2

Explanation: [0, 30] holds one room the whole time while [5, 10] and [15, 20] take turns in a second room — and at time 5 two meetings really are running at once, so one room is not enough.

Example 2:

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

Output: 1

Explanation: The meetings never overlap, so they can share a single room.

Constraints:

  • 1 ≤ intervals.length ≤ 10⁴
  • 0 ≤ start_i < end_i ≤ 10⁶

Hints:

The answer equals the largest number of meetings ever in progress at one moment — you can never use fewer rooms than that, and greedy reuse never needs more.

Concurrency only rises when a meeting starts. So it suffices to count, for each meeting, how many meetings are running at its start time.

Sort by start and keep a min-heap of end times, one entry per occupied room. When a meeting starts at or after the smallest end time, that room is free — reuse it; otherwise open a new one. The heap's final size is the answer.

▶ Run checks these sample cases. Submit also runs hidden edge cases.

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

Expected output: 2