SDE & MLE Prep

253. Meeting Rooms II

Problem

Given meeting time intervals [start, end], return the minimum number of conference rooms needed to hold them all. A room freed at time t can host a meeting starting at t.

Example: [[0,30],[5,10],[15,20]] → 2; [[7,10],[2,4]] → 1

Constraints: up to 10⁴ meetings.

Max overlap at any time. Sweep: sort starts and ends separately; walk starts, freeing a room whenever the earliest end ≤ current start. (Equivalent: min-heap of end times.)

time   0    5   10   15   20   30
       [0----------------------30)
            [5---10)  [15--20)
rooms  1    2    1    2    1        max 2

Complexity: O(n log n).

class Solution:
    def minMeetingRooms(self, intervals: List[List[int]]) -> int:
        starts = sorted(s for s, _ in intervals)
        ends = sorted(e for _, e in intervals)
        rooms = best = j = 0
        for s in starts:
            while ends[j] <= s:
                rooms -= 1
                j += 1
            rooms += 1
            best = max(best, rooms)
        return best