Skip to main content

Meeting Rooms III - Solution & Explanation

HardArrayHash TableSortingHeap (Priority Queue)23 min readAsked at: Amazon, Microsoft, Goldman Sachs +9
Practice this problem

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] = [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:

  1. Each meeting will take place in the unused room with the lowest number.
  2. If there are no available rooms, the meeting will be delayed until a room becomes free. The delayed meeting should have the same duration as the original meeting.
  3. When a room becomes unused, meetings that have an earlier original start time should be given the room.

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 <= 100
  • 1 <= meetings.length <= 105
  • meetings[i].length == 2
  • 0 <= starti < endi <= 5 * 105
  • All the values of starti are unique.

Approach Overview

Problem Overview: You are given n meeting rooms and a list of meeting time intervals. Each meeting must be assigned to the lowest indexed available room. If no room is free, the meeting waits until the earliest room becomes available. After scheduling all meetings, return the room that handled the most meetings.

Approach 1: Priority Queue with Room Availability Tracking (O(m log n) time, O(n) space)

This is the standard optimal approach. First sort meetings by start time using sorting. Maintain two min-heaps: one heap for available rooms (ordered by room index) and another for occupied rooms storing (endTime, roomId). As you iterate through meetings, release rooms whose end time is less than or equal to the current start time. If a room is available, assign the meeting immediately. If not, pop the earliest finishing room and delay the meeting until that time. Track how many meetings each room handles and return the index with the maximum count. Heap operations keep scheduling efficient.

Approach 2: Using Priority Queue for Room Scheduling (O(m log n) time, O(n) space)

This variation focuses on modeling the scheduling process directly. Maintain a min-heap of busy rooms keyed by meeting end time and another heap of free room indices. When processing a meeting, repeatedly pop from the busy heap until rooms become free. If no room is free, extend the meeting start to the earliest end time and reuse that room. This is effectively a scheduling simulation powered by a heap (priority queue). The approach is common in interview discussions because it cleanly separates room selection and meeting completion events.

Approach 3: Simulation with Event Queue (O(m log n) time, O(n) space)

Another perspective treats the problem as a full simulation. Meetings create "start events" and room releases create "end events". A priority queue processes events in chronological order. When a meeting begins, allocate the smallest available room. If none are available, reschedule the meeting when the next room frees up. This approach mirrors real-world scheduling systems and helps when implementing the logic in languages like C++ with explicit event handling.

Approach 4: Sorting and Greedy Allocation (O(m log n) time, O(n) space)

A greedy strategy works after sorting meetings by start time. Always assign the earliest available room with the smallest index. If all rooms are busy, greedily select the meeting that finishes first and extend its timeline. Internally this still relies on a heap to efficiently track the earliest finishing meeting, but the reasoning focuses on greedy scheduling rather than explicit simulation. Concepts from array processing and interval scheduling drive the logic.

Recommended for interviews: The priority queue with room availability tracking is what most interviewers expect. It demonstrates understanding of interval scheduling, heaps, and simulation. Starting with a basic simulation shows you understand the constraints, but implementing the two-heap optimal approach proves you can design scalable scheduling systems.

Approach 1: Priority Queue with Room Availability Tracking

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.

Code

Python

Complexity

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.

Try this approach in the editor →

Approach 2: Simulation with Event Queue

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.

Code

C++

Complexity

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.

Try this approach in the editor →

Approach 3: Using Priority Queue for Room Scheduling

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.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

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.

Try this approach in the editor →

Approach 4: Sorting and Greedy Allocation

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.

Code

C

C++

Complexity

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.

Try this approach in the editor →

Approach 5: Priority Queue (Min-Heap)

We define two priority queues to represent idle meeting rooms and busy meeting rooms respectively. The idle meeting rooms idle are sorted by index; the busy meeting rooms busy are sorted by end time and index.

First, sort the meetings by start time, then iterate through the meetings. For each meeting:

  • If there are busy meeting rooms with end time less than or equal to the current meeting's start time, add them to the idle meeting rooms queue idle;
  • If there are idle meeting rooms available, take the room with the smallest index from the idle queue idle and add it to the busy queue busy;
  • If there are no idle meeting rooms, find the room with the earliest end time and smallest index from the busy queue busy, and add it back to the busy queue busy.

The time complexity is O(m (log m + log n)), and the space complexity is O(n + m), where n and m are the number of meeting rooms and meetings respectively.

Similar problems:

Code

Python

Java

C++

Go

TypeScript

Try this approach in the editor →

Complexity Comparison

ApproachComplexity
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).
Space Complexity: O(n + m), used for storing the room_heap and end_times.

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.
Space Complexity: O(n + m), for the available rooms queue, ongoing meetings queue, and usage tracker.

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.
Space Complexity: O(n), where n is the number of rooms, for maintaining the arrays for booking and 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.
Space Complexity: O(n), as it maintains end times and booking counts for each room.

Priority Queue (Min-Heap)—

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Priority Queue with Room Availability TrackingO(m log n)O(n)Best general solution for interviews and production scheduling problems
Priority Queue Room SchedulingO(m log n)O(n)When modeling busy and free rooms separately for clarity
Simulation with Event QueueO(m log n)O(n)Useful when implementing a full event-driven simulation
Sorting and Greedy AllocationO(m log n)O(n)When reasoning about interval scheduling and greedy strategies

Video Solution

Meeting Rooms III - Leetcode 2402 - Python • NeetCodeIO • 37,355 views views

Watch 9 more video solutions →

Frequently Asked Questions

Is Meeting Rooms III easy or hard?
Meeting Rooms III is classified as a Hard problem on LeetCode. The difficulty comes from combining interval scheduling, heap-based resource management, and simulation while correctly handling delayed meetings.
Meeting Rooms III Python or Java solution
Most implementations use two heaps. In Python, the heapq module manages available and busy rooms efficiently. Java solutions typically rely on PriorityQueue with custom comparators for end times and room indices.
How to solve Meeting Rooms III in O(m log n)?
Sort the meetings by start time, then maintain two heaps: one for available rooms ordered by index and another for occupied rooms ordered by end time. Before scheduling a meeting, release rooms whose meetings have ended. If no room is free, delay the meeting until the earliest room becomes available. Each scheduling step uses heap operations, giving O(m log n) complexity.
What is the best approach for Meeting Rooms III?
The optimal solution uses two priority queues. One heap stores available room indices and another tracks busy rooms by their meeting end time. Meetings are processed in sorted order, freeing rooms when their end time passes. This approach schedules each meeting with efficient heap operations and runs in O(m log n) time.
What data structure is used in Meeting Rooms III?
The core data structure is a min-heap (priority queue). One heap tracks the earliest finishing meeting and another keeps available room indices sorted. Arrays are used to count how many meetings each room handles.
What is the time complexity of Meeting Rooms III?
The dominant cost comes from heap operations during scheduling. After sorting the meetings in O(m log m), each meeting may trigger push and pop operations on heaps of size at most n. The overall complexity is O(m log n) time with O(n) extra space.
Is Meeting Rooms III asked at Google, Amazon, or Meta?
Meeting room scheduling and interval allocation problems appear frequently in interviews at companies like Google, Amazon, and Meta. Variants of Meeting Rooms I, II, and III are common because they test heaps, greedy scheduling, and simulation skills in one problem.

Ready to solve this problem?

Practice Meeting Rooms III with our built-in code editor and test cases.

Practice on FleetCode