Problem · Array
Meeting Rooms II
Learn this problemProblem statement
Given an array of meeting time intervals consisting of start and end times [[start_1, end_1], [start_2, end_2], ...] where start_i < end_i, find the minimum number of meeting rooms required to schedule all meetings without any conflicts.
Intervals are half-open: a meeting that ends exactly when another begins does not conflict. For example, (0, 8) and (8, 10) are not considered a conflict at time 8 and can share a room.
A common approach sorts the start and end times (or uses a min-heap of meeting end times): sweep the events in time order, and track the maximum number of meetings that are simultaneously in progress, which is the number of rooms required.
Function
minMeetingRooms(intervals: int[][]) → intExamples
Example 1
intervals = [[0, 40], [5, 10], [15, 20]]return = 2Room 1 holds (0, 40). Room 2 holds (5, 10) and then (15, 20), which do not overlap each other. The meeting (0, 40) overlaps both of the others, so a second room is required, giving 2 rooms total.
Example 2
intervals = [[4, 9]]return = 1There is a single meeting, so exactly one room is needed.
Example 3
intervals = [[0, 8], [8, 10]]return = 1The first meeting ends at 8 exactly when the second begins. Half-open intervals do not conflict at the shared endpoint, so both fit in a single room.
Constraints
0 <= intervals.length <= 500intervals[i].length == 20 <= intervals[i][0] < intervals[i][1] <= 1000000- An interval that ends exactly when another starts does not count as a conflict.