Quick Overview

Given meetings as half-open start and end time intervals, compute the minimum number of rooms needed so that no room ever hosts two overlapping meetings. Tests interval reasoning, correct handling of back-to-back meetings that share an endpoint, and an efficient approach for up to 100,000 meetings.

Minimum Number of Rooms to Host a Set of Possibly Overlapping Meetings

Company: ByteDance

Role: Machine Learning Engineer

Category: Coding & Algorithms

Difficulty: hard

Interview Round: Technical Screen

You are given the start and end times of a set of meetings. Every meeting must be held in a room, and a room can host only one meeting at a time. Return the minimum number of rooms needed to hold all of the meetings. ### Function Signature ```python def min_rooms(meetings: list[tuple[int, int]]) -> int: ``` Each meeting is a pair `(start, end)`. ### Rules - A meeting occupies its room during the half-open interval `[start, end)`: from `start` up to, but not including, `end`. - Two meetings can use the same room only if their intervals do not overlap. A meeting that ends at time `t` and another that starts at time `t` do not overlap, so one room can host them back to back. - Meetings are given in no particular order, and identical meetings can appear more than once; each occurrence is a separate meeting that needs its own room time. - Return `0` if there are no meetings. ### Constraints - `0 <= len(meetings) <= 10^5` - `0 <= start < end <= 10^9` for every meeting ### Examples **Example 1** ```text Input: meetings = [(1, 5), (2, 6), (4, 8), (6, 9)] Output: 3 ``` At time 4, the meetings `(1, 5)`, `(2, 6)` and `(4, 8)` are all in progress, so at least three rooms are needed. Three are enough: `(6, 9)` starts after `(1, 5)` has ended and can use its room. **Example 2** ```text Input: meetings = [(1, 3), (3, 5), (5, 7)] Output: 1 ``` Each meeting starts exactly when the previous one ends, so one room hosts all three. **Example 3** ```text Input: meetings = [(10, 20), (10, 20), (20, 30)] Output: 2 ``` The two identical meetings need separate rooms, and `(20, 30)` can reuse either of them.

Overview: Given meetings as half-open start and end time intervals, compute the minimum number of rooms needed so that no room ever hosts two overlapping meetings. Tests interval reasoning, correct handling of back-to-back meetings that share an endpoint, and an efficient approach for up to 100,000 meetings.

You are given the start and end times of a set of meetings. Every meeting must be held in a room, and a room can host only one meeting at a time. Return the minimum number of rooms needed to hold all of the meetings. Implement `min_rooms(meetings)`, where `meetings` is a list of pairs `[start, end]`, one pair per meeting. The function returns a single integer. ### Rules - A meeting occupies its room during the half-open interval `[start, end)`: from `start` up to, but not including, `end`. - Two meetings can use the same room only if their intervals do not overlap. A meeting that ends at time `t` and another that starts at time `t` do not overlap, so one room can host them back to back. - Meetings are given in no particular order, and identical meetings can appear more than once; each occurrence is a separate meeting that needs its own room time. - Return `0` if there are no meetings. All times and the answer fit in a signed 32-bit integer. ### Constraints - `0 <= len(meetings) <= 10^5` - `0 <= start < end <= 10^9` for every meeting ### Examples **Example 1** ```text Input: meetings = [[1, 5], [2, 6], [4, 8], [6, 9]] Output: 3 ``` At time 4, the meetings `[1, 5]`, `[2, 6]` and `[4, 8]` are all in progress, so at least three rooms are needed. Three are enough: `[6, 9]` starts after `[1, 5]` has ended and can use its room. **Example 2** ```text Input: meetings = [[10, 20], [10, 20], [20, 30]] Output: 2 ``` The two identical meetings need separate rooms, and `[20, 30]` can reuse either of them, because a meeting that ends at 20 does not overlap one that starts at 20.

Constraints

  • 0 <= len(meetings) <= 10^5
  • 0 <= start < end <= 10^9 for every meeting
  • Each meeting is a pair [start, end] occupying the half-open interval [start, end)
  • Meetings are in no particular order; identical meetings may repeat and each occurrence counts separately
  • All times and the answer fit in a signed 32-bit integer

Examples

Input: ([],)

Expected Output: 0

Explanation: No meetings need no rooms.

Input: ([[3, 7]],)

Expected Output: 1

Explanation: A single meeting needs exactly one room.

Hints

  1. At any moment, every meeting in progress needs its own room. What lower bound does that give, and can it always be met?
  2. Because the intervals are half-open, a meeting that ends at time t and one that starts at time t can share a room. Make sure equal times are handled that way.
  3. The input is unsorted and may contain identical meetings; each copy is a separate meeting.

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. For each start s, if s >= ends[j], some meeting has already ended by time s (intervals are half-open, so an end at s frees its room for a start at s); the new meeting reuses that room and j advances. Otherwise every opened room is busy and a new room is opened. The answer is the number of rooms opened.

Invariant and correctness: let E(s) be the number of end times <= s. After processing the i-th start s_i (0-based), j = min(j_prev + 1, E(s_i)), so rooms = max(rooms_prev, (i + 1) - E(s_i)). At the last start equal to a time t, (i + 1) - E(t) is exactly the number of meetings whose interval [start, end) contains t; at earlier starts equal to t the value is smaller. The number of meetings in progress only increases at a start time, so the final count equals the peak number of simultaneous meetings. That peak is a lower bound (those meetings pairwise overlap and need distinct rooms), and it is achievable: assigning meetings in start order to any room whose previous meeting has ended never needs more rooms than the peak. The pointer is always in range because j grows by at most one per processed start, so j <= i < n.

Edge cases: an empty list returns 0 without entering the loop; identical meetings are separate entries in both sorted lists, so each copy opens or reuses its own room; an end and a start at the same time resolve as a reuse because the comparison is >=; input order is irrelevant because both lists are sorted. Times are at most 10^9 and the answer at most 10^5, so 32-bit integers suffice in every language.

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