Meeting Rooms III - Complete Solution Guide
Meeting Rooms III is LeetCode problem 2402, a Hard level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
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] = [start i , end i ] means that a meeting will be held during the half-closed time interval [start i , end i ) . All the values of start i are unique . Meetings are allocated to rooms in the following manner: Each meeting will take place in the unused room with the lowest number. If there are no available rooms, the meeting will be delayed until a room becomes fre
Detailed Explanation
The problem simulates a meeting room allocation scenario. Given 'n' rooms and a list of meetings with start and end times, the goal is to determine which room hosted the most meetings. Meetings are assigned to the lowest-numbered available room. If no rooms are available, the meeting is delayed until a room becomes free, maintaining the original meeting duration. The room that held the most meetings (and the lowest-numbered one in case of a tie) should be returned.
Solution Approach
The solution uses a simulation approach, processing meetings one by one in chronological order. It employs two priority queues: 'available_rooms' to store available rooms (sorted by room number) and 'occupied_rooms' to store rooms currently in use (sorted by end time). For each meeting, the algorithm first frees up any rooms that have finished their meetings. Then, if available rooms exist, the meeting is assigned to the lowest-numbered available room. Otherwise, the meeting is delayed until the earliest-finishing room becomes free, and the end time is adjusted accordingly. Finally, the room that hosted the most meetings is determined.
Step-by-Step Algorithm
- Step 1: Sort the meetings array based on their start times in ascending order.
- Step 2: Initialize a min-heap 'available_rooms' with room numbers from 0 to n-1.
- Step 3: Initialize a min-heap 'occupied_rooms' to store rooms that are currently occupied, sorted by their end times. The heap will store pairs of (end_time, room_number).
- Step 4: Initialize an array 'room_counts' to keep track of the number of meetings held in each room.
- Step 5: Iterate through the sorted meetings array.
- Step 6: For each meeting, check if any rooms in 'occupied_rooms' have become available (end time <= current meeting's start time). If so, move those rooms to 'available_rooms'.
- Step 7: If 'available_rooms' is not empty, assign the meeting to the room with the lowest number from 'available_rooms', add the room and the end time to 'occupied_rooms', and increment the corresponding count in 'room_counts'.
- Step 8: If 'available_rooms' is empty, delay the meeting until the earliest-finishing room becomes available. Pop the room from 'occupied_rooms', calculate the new end time for the delayed meeting, push the room and the new end time back into 'occupied_rooms', and increment the corresponding count in 'room_counts'.
- Step 9: After processing all meetings, iterate through 'room_counts' to find the room with the most meetings. Return the index of that room.
Key Insights
- Insight 1: Sorting the meetings by start time allows processing them in chronological order, which is crucial for simulating the allocation process correctly.
- Insight 2: Using a min-heap (priority queue) for both available rooms and occupied rooms efficiently manages the allocation and deallocation of rooms based on their availability and end times.
- Insight 3: Handling the case where no rooms are available requires delaying the meeting and updating the end time of the room that becomes available earliest, using the original meeting duration.
Complexity Analysis
Time Complexity: O(m log n)
Space Complexity: O(n)
Topics
This problem involves: Array, Hash Table, Sorting, Heap (Priority Queue), Simulation.
Companies
Asked at: PayPal, Pinterest.