Given an array consisting of meeting time intervals, which are represented by the start and end times ([s0, e0], [s1, e1], ...], determine the minimum number of meeting rooms needed in order to be able to accommodate all meetings at every moment of time.
Example:
Input: [[5, 10], [0, 30], [15, 20]] Output: 2 Explanation: 1st and 3rd meetings can be assigned to the first room 2nd meeting can be assigned to room 2
Note:
Your algorithm should run in O(n log n) time and use O(n) extra space.
The core challenge of this problem is to determine the minimum number of meeting rooms required to accommodate all meetings without any overlap. This problem is significant in scheduling and resource allocation scenarios, such as booking conference rooms or managing CPU tasks.
Potential pitfalls include misunderstanding the overlap conditions and not accounting for meetings that end exactly when another starts.
To solve this problem, we can use a greedy algorithm with a priority queue (min-heap). Here's a step-by-step approach:
Here's a detailed breakdown of the algorithm:
/**
* @param {number[][]} intervals
* @return {number}
*/
function minMeetingRooms(intervals) {
if (intervals.length === 0) {
return 0;
}
// Sort the intervals by start time
intervals.sort((a, b) => a[0] - b[0]);
// Min-heap to track the end times of meetings
const heap = [];
// Add the end time of the first meeting
heap.push(intervals[0][1]);
for (let i = 1; i < intervals.length; i++) {
// If the room due to free up the earliest is free, remove it from the heap
if (heap[0] <= intervals[i][0]) {
heap.shift();
}
// Add the current meeting's end time to the heap
heap.push(intervals[i][1]);
// Re-sort the heap
heap.sort((a, b) => a - b);
}
// The size of the heap is the number of rooms required
return heap.length;
}
// Example usage:
const intervals = [[5, 10], [0, 30], [15, 20]];
console.log(minMeetingRooms(intervals)); // Output: 2
The time complexity of this solution is O(n log n) due to the sorting step and the heap operations. The space complexity is O(n) because we store all end times in the heap.
Consider the following edge cases:
To test the solution comprehensively, consider the following test cases:
// Test case 1: No meetings
console.log(minMeetingRooms([])); // Output: 0
// Test case 2: Meetings that do not overlap
console.log(minMeetingRooms([[1, 2], [3, 4], [5, 6]])); // Output: 1
// Test case 3: Meetings that overlap
console.log(minMeetingRooms([[1, 4], [2, 5], [7, 9]])); // Output: 2
// Test case 4: Meetings that start and end at the same time
console.log(minMeetingRooms([[1, 2], [2, 3], [3, 4]])); // Output: 1
When approaching such problems, consider the following tips:
In this blog post, we discussed how to determine the minimum number of meeting rooms required to accommodate all meetings using a greedy algorithm with a priority queue. We covered the problem definition, approach, algorithm, code implementation, complexity analysis, edge cases, and testing. Understanding and solving such problems is crucial for efficient resource management and scheduling.
For further reading and practice, consider the following resources: