Meeting Rooms II - Problem
You're managing a busy conference center and need to determine the minimum number of conference rooms required to accommodate all scheduled meetings without any conflicts.
Given an array of meeting time intervals intervals where intervals[i] = [starti, endi], your task is to find the minimum number of conference rooms needed so that no two meetings overlap in the same room.
Key Points:
- Meetings with the same start and end time as another meeting's end and start time do not overlap
- A meeting that ends at time
tand another that starts at timetcan use the same room - You need to return the minimum number of rooms required
Example: If you have meetings [[0,30],[5,10],[15,20]], you need 2 rooms because the first meeting (0-30) overlaps with both other meetings.
Input & Output
example_1.py โ Basic Overlap
$
Input:
intervals = [[0,30],[5,10],[15,20]]
โบ
Output:
2
๐ก Note:
Meeting [0,30] overlaps with both [5,10] and [15,20], but [5,10] and [15,20] don't overlap with each other. So we need 2 rooms: one for [0,30] and another that can be shared by [5,10] and [15,20].
example_2.py โ Adjacent Meetings
$
Input:
intervals = [[7,10],[2,4]]
โบ
Output:
1
๐ก Note:
These meetings don't overlap - one ends at 4 and the other starts at 7, so they can share the same room sequentially.
example_3.py โ All Overlapping
$
Input:
intervals = [[9,10],[4,9],[4,17]]
โบ
Output:
2
๐ก Note:
[4,9] and [9,10] don't overlap (first ends when second starts), but [4,17] overlaps with both. Maximum concurrent meetings is 2, so we need 2 rooms.
Visualization
Tap to expand
Understanding the Visualization
1
Receive booking requests
You get meeting requests with start and end times, similar to hotel check-in/check-out
2
Sort by arrival time
Process meetings chronologically by start time, like handling hotel guests as they arrive
3
Track room availability
Use a system (heap) to efficiently track when each occupied room becomes free
4
Optimize room usage
Always reuse the earliest available room to minimize total rooms needed
Key Takeaway
๐ฏ Key Insight: The minimum number of conference rooms needed equals the maximum number of concurrent meetings at any point in time. Using a min-heap to efficiently track room availability gives us the optimal O(n log n) solution.
Time & Space Complexity
Time Complexity
O(nยฒ)
For each of n meetings, we might check against all previously placed meetings in worst case
โ Quadratic Growth
Space Complexity
O(n)
We store all meetings distributed across different rooms
โก Linearithmic Space
Constraints
- 1 โค intervals.length โค 104
- 0 โค starti < endi โค 106
- All meeting times are non-negative integers
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code