Quick Overview

Given meeting start and end times as half-open intervals, return the minimum number of rooms needed so that no two overlapping meetings share a room. It tests interval reasoning, sorting and careful handling of meetings that end exactly when others start.

Minimum number of meeting rooms for a set of meetings

Company: Microsoft

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: hard

Interview Round: Onsite

Given the start and end times of a set of meetings, return the minimum number of rooms needed to hold all of them, where a room can host only one meeting at a time. The report describes this round as a variant of the classic minimum-rooms problem without saying what the variant changed; this is the base problem. ### Function Signature ```python def min_meeting_rooms(meetings: list[list[int]]) -> int: ``` ### Rules - `meetings[i] = [start, end]` occupies a room for the half-open interval `[start, end)`: the room is in use at every time `t` with `start <= t < end`. - A meeting that ends at time `t` frees its room for a meeting that starts at time `t`. - Identical meetings are separate meetings, and each needs its own room. - Return `0` when there are no meetings. ### Constraints - `0 <= len(meetings) <= 10^5` - `0 <= start < end <= 10^9` for every meeting - Times are integers, so every value fits in a 32-bit signed integer. - `meetings` is in no particular order. ### Examples **Example 1** ```text Input: meetings = [[9, 12], [10, 13], [11, 12], [12, 14]] Output: 3 ``` At time `11`, the meetings `[9, 12)`, `[10, 13)` and `[11, 12)` are all in progress. At time `12`, two of them have ended, so `[12, 14)` can reuse a room. **Example 2** ```text Input: meetings = [[1, 5], [5, 8], [8, 9]] Output: 1 ``` Each meeting starts exactly when the one before it ends, so a single room hosts all three. **Example 3** ```text Input: meetings = [[4, 6], [1, 10], [6, 8], [2, 3]] Output: 2 ``` The meeting `[1, 10)` keeps one room busy throughout. The other three never overlap one another, so a second room hosts them in turn.

Overview: Given meeting start and end times as half-open intervals, return the minimum number of rooms needed so that no two overlapping meetings share a room. It tests interval reasoning, sorting and careful handling of meetings that end exactly when others start.

Given the start and end times of a set of meetings, return the minimum number of rooms needed to hold all of them. A room can host only one meeting at a time. Implement `min_meeting_rooms(meetings)`, where `meetings[i] = [start, end]`, and return the minimum number of rooms as an integer. ### Rules - Meeting `[start, end]` occupies a room for the half-open interval `[start, end)`: the room is in use at every time `t` with `start <= t < end`. - A meeting that ends at time `t` frees its room for a meeting that starts at time `t`. - Identical meetings are separate meetings, and each needs its own room. - Return `0` when there are no meetings. ### Constraints - `0 <= len(meetings) <= 10^5` - Each `meetings[i]` is a pair `[start, end]` of integers. - `0 <= start < end <= 10^9` for every meeting - Times are integers, so every value fits in a 32-bit signed integer; the answer is at most `10^5`, so no value exceeds `2^31 - 1` (Java and C++ use `int`). - `meetings` is in no particular order. ### Example 1 ```text Input: meetings = [[9, 12], [10, 13], [11, 12], [12, 14]] Output: 3 ``` At time `11`, the meetings `[9, 12)`, `[10, 13)` and `[11, 12)` are all in progress. At time `12`, two of them have ended, so `[12, 14)` can reuse a room. ### Example 2 ```text Input: meetings = [[4, 6], [1, 10], [6, 8], [2, 3]] Output: 2 ``` The meeting `[1, 10)` keeps one room busy throughout. The other three never overlap one another (`[4, 6)` ends exactly when `[6, 8)` starts), so a second room hosts them in turn.

Constraints

  • 0 <= len(meetings) <= 10^5
  • Each meetings[i] is a pair [start, end] of integers
  • 0 <= start < end <= 10^9 for every meeting
  • Times are integers, so every value fits in a 32-bit signed integer (no value exceeds 2^31 - 1)
  • meetings is in no particular order

Examples

Input: ([],)

Expected Output: 0

Explanation: No meetings need no rooms (minimum valid input).

Input: ([[5, 10]],)

Expected Output: 1

Explanation: A single meeting needs exactly one room.

Hints

  1. A meeting [start, end) is in progress at time t only when start <= t < end, so a meeting ending at t and one starting at t never need a room at the same moment.
  2. The list can arrive shuffled or even reverse-sorted, so do not rely on the input order.
  3. Ask how many meetings can be in progress at a single instant, and how that number relates to the number of rooms.

Loading coding console...

Show the approach

Approach

Algorithm: sort all start times and, separately, all end times. Sweep the starts in increasing order with a pointer j into the sorted ends. Before counting a start s, advance j past every end <= s, decrementing the in-use count for each one; then count s and record the largest in-use value seen.

Invariant: after handling the start s at sorted position i (0-based), in_use = (i + 1) - E(s), where E(s) is the number of end times <= s. When several meetings share the start s, the last of them gives exactly the number of meetings in progress at time s (start <= s < end); the earlier copies give smaller values, so they never raise the maximum.

Correctness: the answer equals the largest number of meetings in progress at one instant. It cannot be smaller, because meetings in progress at the same instant need distinct rooms. That many rooms always suffice: taking meetings in start order and placing each one in any room whose meeting has already ended (end <= its start) never runs out, since a fresh room is needed only when every open room is busy at that start. The in-progress count rises only at a start time, so its maximum is reached at some start, and the sweep evaluates every start. Comparing ends with <= encodes the half-open rule: a meeting ending at t frees its room before a meeting starting at t takes one. Every end <= s belongs to a meeting whose start is < s, so it has already been counted, j never overtakes the starts, and in_use never goes negative.

Edge cases: an empty list skips the loop and returns 0; a single meeting returns 1; k identical meetings return k because none of their ends is <= their shared start; input order is irrelevant because both time lists are sorted; all times are at most 10^9 and the answer is at most 10^5, so 32-bit integers suffice.

Time complexity:
O(n log n)
Space complexity:
O(n)