You are given a list of meeting time intervals, where each interval consists of a start time and an end time. Your task is to determine the minimum number of conference rooms required to accommodate all the meetings without any overlap. Assume that a meeting room can be reused immediately after a meeting ends.
Example 1:
Input: [[0, 30],[5, 10],[15, 20]]
Output: 2
Explanation: Two conference rooms are required.
Example 2:
Input: [[7,10],[2,4]]
Output: 1
Explanation: One conference room is sufficient since the meetings do not overlap.
Example 3:
Input: [[1,8],[6,20],[9,16],[13,17]]
Output: 3
Write a function that takes the array of meeting intervals as input and returns the minimum number of conference rooms required.
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.
For example, given the intervals [[0, 30],[5, 10],[15, 20]]
, the output should be 2.
A brute-force solution would involve checking each interval against all other intervals to see if they overlap. For each interval, we'd count how many other intervals overlap with it. The maximum number of overlaps any interval has would be the minimum number of meeting rooms required.
This approach is inefficient because, for each interval, we iterate through the entire list of intervals.
O(n^2), where n is the number of intervals, due to the nested loops used to compare each interval with every other interval.
O(1), as we only use a constant amount of extra space.
The optimal solution involves sorting the intervals based on their start times. Then, we use a min-heap to track the end times of the meetings currently in progress. We iterate through the sorted intervals. For each interval, we check if the meeting room freed up. if it is freed up we remove the meeting end time from the heap. We keep a counter for tracking the maximum number of meeting rooms required.
O(n log n), where n is the number of intervals. Sorting the intervals takes O(n log n) time, and iterating through the intervals and performing heap operations (insertion and deletion) takes O(n log n) time in the worst case.
O(n), where n is the number of intervals. This is because, in the worst-case scenario (when all meetings overlap), the min-heap can contain all the end times of the intervals.
import java.util.Arrays;
import java.util.PriorityQueue;
class Solution {
public int minMeetingRooms(int[][] intervals) {
if (intervals == null || intervals.length == 0) {
return 0;
}
// Sort the intervals by start time
Arrays.sort(intervals, (a, b) -> Integer.compare(a[0], b[0]));
// Use a min-heap to track the end times of meetings
PriorityQueue<Integer> minHeap = new PriorityQueue<>();
// Add the first meeting to the heap
minHeap.add(intervals[0][1]);
// Iterate through the rest of the meetings
for (int i = 1; i < intervals.length; i++) {
// If the current meeting starts after the last meeting ends,
// then we can reuse the room
if (intervals[i][0] >= minHeap.peek()) {
minHeap.poll();
}
// Add the current meeting to the heap
minHeap.add(intervals[i][1]);
}
// The size of the heap is the number of rooms required
return minHeap.size();
}
}