
Problem Statement
Given an array of meeting intervals where intervals[i] = [start<sub>i</sub>, end<sub>i</sub>], return the minimum number of meeting rooms needed so that no meetings overlap.
Examples
Example 1:
- Input: intervals =
[[10, 15], [20, 25], [30, 35]] - Expected Output:
1 - Justification: There are no overlapping intervals in the given list. So, only 1 meeting room is enough for all the meetings.
Example 2:
- Input: intervals =
[[10, 20], [15, 25], [24, 30], [5, 14], [22, 28], [1, 4], [27, 35]] - Expected Output:
3 - Justification:
Let's see how many meetings overlap at the same time:
- [1, 4] starts first.
- Then [5, 14] begins, no overlap yet.
- [10, 20] overlaps with [5, 14]
- [15, 25] overlaps with [10, 20]
- [22, 28] overlaps with [15, 25]
- [24, 30] overlaps with both [22, 28] and [15, 25]
- [27, 35] overlaps with [24, 30]
Example 3:
- Input: intervals =
[[10, 20], [20, 30]] - Expected Output:
1 - Justification: The end time of the first meeting is the same as the start time of the second meeting. So, one meeting can be scheduled right after the other in the same room.
Constraints:
- 1 <= intervals.length <= 10<sup>4</sup>
- 0 <= start<sub>i</sub> < end<sub>i</sub> <= 10<sup>6</sup>
Solution
To find the minimum number of meeting rooms required, we sort all the meetings by their start times. This allows us to always process meetings in chronological order. Then, we use a min-heap (priority queue) to track the end times of meetings that are currently using rooms. The heap helps us know which room becomes available the earliest.
For each meeting, we compare its start time with the earliest ending meeting (top of the heap). If the current meeting can reuse a room (its start time is greater than or equal to the earliest end time), we remove the ended meeting from the heap. Regardless, we then add the current meeting's end time to the heap. At the end, the size of the heap tells us how many rooms were used at the same time - which is the minimum number of rooms required.
Step-by-Step Algorithm
-
Check if there are no meetings:
- If the input array
intervalsis empty, return0since no rooms are needed.
- If the input array
-
Sort the meetings by their start times:
- Use
Arrays.sort()and sort all intervals based on the first element (start time).
- Use
-
Create a min-heap (priority queue):
- Initialize a
PriorityQueueto keep track of ongoing meetings. - This heap will store the end times of meetings currently using a room.
- The meeting that ends the earliest will always be at the top.
- Initialize a
-
Add the first meeting's end time to the heap:
- This allocates the first room for the first meeting.
-
Process the remaining meetings one by one:
- For each meeting from the second onwards:
- Compare its start time with the top of the heap (the earliest end time).
- If the current meeting starts after or exactly when the earliest meeting ends:
- Remove the top value from the heap (free up the room).
- Add the current meeting's end time to the heap (assign a room).
- For each meeting from the second onwards:
-
Return the number of rooms used:
- After processing all meetings, return the size of the heap.
- This number represents the maximum number of rooms used at the same time, which is the minimum number of rooms required.
- The heap can never shrink. Each meeting removes at most one end time and then always adds one, so the size either stays the same or grows by one. That is why the size at the end is also the largest size the heap ever reached, and there is no need to track a separate maximum while looping.
Algorithm Walkthrough
Let's trace the second example, [[10,20], [15,25], [24,30], [5,14], [22,28], [1,4], [27,35]]. Move through the steps one at a time:
Step 1. Sort the meetings by start time so they are handled in the order they begin. For each one, the only question is whether some room is already free, and the only room worth checking is the one that frees up soonest. So the rooms in use are kept as a row of their end times with the earliest in front, and nothing else about them is remembered. The number of rooms open at the end is the answer.
1 of 9
Code
Complexity Analysis
-
Time Complexity: The time complexity of our algorithm is O(N \log N), where
Nis the number of intervals. This is because we're sorting the intervals once and then using priority queues to process them. -
Space Complexity: The space complexity is O(N) as we're storing all intervals in the worst case.