You are given an array 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 overlaps. Each meeting needs its own room during the interval, and a room can be reused only after the previous meeting has ended.
Input: An array of meeting time intervals, represented as int[][] intervals
, where intervals[i] = [start_time, end_time]
. Assume start_time < end_time
for each interval.
Examples:
Input: intervals = [[0, 30], [5, 10], [15, 20]]
Output: 2
Explanation:
Input: intervals = [[7, 10], [2, 4]]
Output: 1
Explanation:
Input: intervals = [[1, 5], [8, 9], [8, 9]]
Output: 2
Explanation:
Constraints:
0 <= intervals.length <= 10000
intervals[i].length == 2
0 <= intervals[i][0] < intervals[i][1] <= 10^6
Can you write a function to efficiently calculate 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 [[0, 30],[5, 10],[15, 20]]
, return 2
.
A naive solution would be to iterate through all the intervals and for each interval, check if it overlaps with any of the existing rooms. If it doesn't overlap, we can assign it to an existing room. If it overlaps with all existing rooms, we need to create a new room. This approach is inefficient because it requires comparing each interval with all existing rooms.
The optimal solution involves sorting the intervals by their start times and then using a min-heap to track the end times of the meetings in progress. The min-heap allows us to efficiently find the meeting that ends earliest.
Here's a step-by-step breakdown:
import java.util.Arrays;
import java.util.PriorityQueue;
public class MeetingRoomsII {
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 merged intervals
PriorityQueue<Integer> minHeap = new PriorityQueue<>();
// Add the first meeting to the heap
minHeap.add(intervals[0][1]);
// Iterate through the remaining meetings
for (int i = 1; i < intervals.length; i++) {
// If the current meeting starts after the earliest ending meeting,
// then we can reuse a room
if (intervals[i][0] >= minHeap.peek()) {
minHeap.poll();
}
// Add the current meeting's end time to the heap
minHeap.add(intervals[i][1]);
}
// The size of the heap is the number of rooms required
return minHeap.size();
}
public static void main(String[] args) {
MeetingRoomsII meetingRooms = new MeetingRoomsII();
// Example usage
int[][] intervals1 = {{0, 30}, {5, 10}, {15, 20}};
System.out.println("Minimum meeting rooms required: " + meetingRooms.minMeetingRooms(intervals1)); // Output: 2
int[][] intervals2 = {{7,10},{2,4}};
System.out.println("Minimum meeting rooms required: " + meetingRooms.minMeetingRooms(intervals2)); // Output: 1
int[][] intervals3 = {{1,5},{8,9},{8,9}};
System.out.println("Minimum meeting rooms required: " + meetingRooms.minMeetingRooms(intervals3)); // Output: 2
}
}
Therefore, the overall time complexity is O(n log n).