You are given an integer n. There are n rooms numbered from 0 to n - 1.
You are given a 2D integer array meetings where meetings[i] = [starti, endi] means that a meeting will be held during the half-closed time interval [starti, endi). All the values of starti are unique.
Meetings are allocated to rooms in the following manner:
Return the number of the room that held the most meetings. If there are multiple rooms, return the room with the lowest number.
A half-closed interval [a, b) is the interval between a and b including a and not including b.
Example 1:
Input: n = 2, meetings = [[0,10],[1,5],[2,7],[3,4]] Output: 0 Explanation: - At time 0, both rooms are not being used. The first meeting starts in room 0. - At time 1, only room 1 is not being used. The second meeting starts in room 1. - At time 2, both rooms are being used. The third meeting is delayed. - At time 3, both rooms are being used. The fourth meeting is delayed. - At time 5, the meeting in room 1 finishes. The third meeting starts in room 1 for the time period [5,10). - At time 10, the meetings in both rooms finish. The fourth meeting starts in room 0 for the time period [10,11). Both rooms 0 and 1 held 2 meetings, so we return 0.
Example 2:
Input: n = 3, meetings = [[1,20],[2,10],[3,5],[4,9],[6,8]] Output: 1 Explanation: - At time 1, all three rooms are not being used. The first meeting starts in room 0. - At time 2, rooms 1 and 2 are not being used. The second meeting starts in room 1. - At time 3, only room 2 is not being used. The third meeting starts in room 2. - At time 4, all three rooms are being used. The fourth meeting is delayed. - At time 5, the meeting in room 2 finishes. The fourth meeting starts in room 2 for the time period [5,10). - At time 6, all three rooms are being used. The fifth meeting is delayed. - At time 10, the meetings in rooms 1 and 2 finish. The fifth meeting starts in room 1 for the time period [10,12). Room 0 held 1 meeting while rooms 1 and 2 each held 2 meetings, so we return 1.
Constraints:
1 <= n <= 1001 <= meetings.length <= 105meetings[i].length == 20 <= starti < endi <= 5 * 105starti are unique.This approach involves using a priority queue (min-heap) to manage room availability and another priority queue to track which meetings are currently taking place. The idea is to process the meetings in order of their start times, allocating them to the earliest available room when possible.
We use two heaps: one to keep track of the earliest finish time of meetings in each room, and another to handle room availability. This ensures that delayed meetings are processed as soon as a room becomes free.
The solution sorts the meetings and uses two heaps: room_heap to track available room numbers and end_times to track when rooms will become free. We loop through the sorted meetings and allocate them either to an immediately available room or delay them as needed. The room usage counter is updated each time a room is assigned a meeting.
Time Complexity: O((m + n) * log(n)), where m is the number of meetings and n is the number of rooms. Sorting the meetings takes O(m log m), and handling the heaps for each meeting takes O(log n).
Space Complexity: O(n + m), used for storing the room_heap and end_times.
This method simulates time progression by using an event queue to handle meeting starting and ending events. The key is to manage events such that at any point, we know which room becomes available next, and we can decide room allocation accordingly.
We use a heap to prioritize events primarily by time, then by starting or ending to handle adjustments when a room frees up.
The solution processes meeting events by sorting them and using two priority queues: availableRooms to track available room numbers and ongoingMeetings to track meetings that are currently taking place. The simulation iterates over sorted meeting times to either assign a room immediately or adjust its start based on available room times.
Time Complexity: O((m + n) * log(n)), where m is the number of meetings and n is the number of rooms. Sorting meetings takes O(m log m), and each meeting's start/end is handled in O(log n) due to the priority queues.
Space Complexity: O(n + m), for the available rooms queue, ongoing meetings queue, and usage tracker.
This approach leverages a priority queue to efficiently manage room availability and meeting allocation. We will sort the meetings by their start time. As we iterate through these meetings, we use a priority queue to track the rooms and their next available time. The queue helps in quickly finding the soonest available room, or if a room is currently available. This allows us to allocate or delay the meetings accordingly.
This C program utilizes arrays to simulate a priority-based room allocation system. The array availableRooms maintains the next available time for each room, and bookings counts the number of meetings each room hosts. Meetings are scheduled or delayed based on room availability.
C++
Java
Python
C#
JavaScript
Time Complexity: O(m * n), where m is the number of meetings and n is the number of rooms, mainly due to iterating over the rooms to find availability.
Space Complexity: O(n), where n is the number of rooms, for maintaining the arrays for booking and availability.
This method involves sorting and maintaining room states via a greedy algorithm. We prioritize actively managing room occupation intervals without relying on complex data structures, focusing on maintaining an iterative timeline across sorted meetings to achieve room bookings.
This C implementation sorts the meetings by start time, then assigns them to rooms using a simple iterative calculation of earliest room availability or delaying the meeting start if necessary. It maintains counters for each room to identify which one is used most.
C++
Time Complexity: O(m * n), where m is the number of meetings and n is the number of rooms due to the iteration over rooms.
Space Complexity: O(n), as it maintains end times and booking counts for each room.
| Approach | Complexity |
|---|---|
| Priority Queue with Room Availability Tracking | Time Complexity: O((m + n) * log(n)), where m is the number of meetings and n is the number of rooms. Sorting the meetings takes O(m log m), and handling the heaps for each meeting takes O(log n). |
| Simulation with Event Queue | Time Complexity: O((m + n) * log(n)), where m is the number of meetings and n is the number of rooms. Sorting meetings takes O(m log m), and each meeting's start/end is handled in O(log n) due to the priority queues. |
| Using Priority Queue for Room Scheduling | Time Complexity: O(m * n), where m is the number of meetings and n is the number of rooms, mainly due to iterating over the rooms to find availability. |
| Sorting and Greedy Allocation | Time Complexity: O(m * n), where m is the number of meetings and n is the number of rooms due to the iteration over rooms. |
Meeting Rooms II - Leetcode 253 - Python • NeetCode • 174,315 views views
Watch 9 more video solutions →Practice Meeting Rooms III with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor