Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],...] (si < ei), find the minimum number of conference rooms required.
Example 1:
Input: intervals = [[0,30],[5,10],[15,20]]
Output: 2
Example 2:
Input: intervals = [[7,10],[2,4]]
Output: 1
Constraints:
1 <= intervals.length <= 1040 <= starti < endi <= 106When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
Imagine you have a list of meetings with different start and end times, and you need to figure out the minimum number of rooms required so that no meetings overlap. The brute force approach tries every possible way of assigning meetings to rooms and checks all combinations until you find the minimum number of rooms that actually works without any scheduling conflicts.
Here's how the algorithm would work step-by-step:
def min_meeting_rooms(meeting_intervals):
# Sort intervals to evaluate meetings chronologically and test combinations systematically
sorted_intervals = sorted(meeting_intervals, key=lambda meeting: meeting[0])
room_allocation_list = []
for current_meeting in sorted_intervals:
room_found = False
for assigned_room in room_allocation_list:
# If the room's last meeting ends before current starts, we can reuse this room safely
if assigned_room[-1][1] <= current_meeting[0]:
assigned_room.append(current_meeting)
room_found = True
break
# If no existing room is free, we must open a brand new room for this meeting
if not room_found:
room_allocation_list.append([current_meeting])
# The total rooms needed equals the maximum number of rooms active during any trial
return len(room_allocation_list)To figure out how many meeting rooms are needed without wasting time checking every single overlap manually, the best strategy is to look at the meetings in chronological order and keep track of when rooms become free. By organizing the start and end times separately, we can simulate walking through the day and simply count how many rooms are occupied at the busiest moment.
Here's how the algorithm would work step-by-step:
def min_meeting_rooms(meeting_intervals):
# Separating start and end times allows us to track timeline events chronologically.
start_times = sorted([interval[0] for interval in meeting_intervals])
end_times = sorted([interval[1] for interval in meeting_intervals])
rooms_currently_used = 0
peak_rooms_needed = 0
start_pointer = 0
end_pointer = 0
total_meetings = len(meeting_intervals)
while start_pointer < total_meetings:
# If a meeting ends before or when the next starts, a room becomes immediately available.
if start_times[start_pointer] >= end_times[end_pointer]:
rooms_currently_used -= 1
end_pointer += 1
rooms_currently_used += 1
start_pointer += 1
# Tracking the maximum concurrent rooms ensures we allocate enough capacity for peak overlap.
if rooms_currently_used > peak_rooms_needed:
peak_rooms_needed = rooms_currently_used
return peak_rooms_needed| Case | How to Handle |
|---|---|
| Empty array of intervals | Return zero rooms immediately since no meetings require a space. |
| Single meeting interval | Return one room since exactly one interval always requires a single conference room. |
| Back-to-back meetings where one ends exactly when another starts | Reuse the same room by checking if the earliest ending meeting time is less than or equal to the current meeting start time. |
| Completely overlapping meetings sharing the exact same start and end times | Allocate separate rooms for each overlapping interval by tracking concurrent active meetings. |
| Meetings with negative time coordinates or zero-duration intervals | Process them correctly by relying purely on numerical start and end comparisons during sorting. |
| Massive scale input with hundreds of thousands of intervals | Maintain efficient O(n log n) time complexity using a min-heap or chronological sorting approach. |
| Inputs with extreme boundary integer values for start and end times | Use standard 32-bit or 64-bit integer types natively supported by the language without causing arithmetic overflow. |