2402. Meeting Rooms III - Detailed Explanation

Free Coding Questions Catalog
Boost your coding skills with our essential coding questions catalog. Take a step towards a better tech career now!

Problem Statement

In the "Meeting Rooms III" problem, you are given an integer n representing the number of meeting rooms (numbered from 0 to n - 1) and an array of meeting requests where each meeting is represented as [start, end]. Each meeting has a fixed duration (end − start). Meetings are scheduled in one of the available rooms according to the following rules:

• If there is a free meeting room at the meeting’s start time, assign the meeting to the room with the smallest room number.

• If no room is available at the meeting’s start time, choose the room that becomes available the earliest. In this case, the meeting is delayed until that room is free; its new start time becomes the room’s available time, and its end time is shifted by the same delay (i.e. the meeting duration stays the same).

Your task is to determine which room holds the most meetings after all meeting requests are processed. If there is a tie, return the room with the smallest number.

Hints

  1. Sorting Meetings:
    Sort the meetings by their start times. This ensures that you process the meeting requests in the order they arrive.

  2. Using Two Priority Queues:
    Use one min-heap (priority queue) for free rooms sorted by room number and another for busy rooms sorted by the time they become free (if two rooms free up at the same time, the room with the smaller number should come first).

  3. Handling Delays:
    When no room is free at a meeting’s start time, pick the busy room that becomes available the earliest. The meeting will then start at that room’s available time, and its end time will be delayed by the duration of the meeting.

  4. Counting Meetings:
    Maintain a counter for each room to track the number of meetings held. Update the count every time a meeting is assigned to a room.

Approaches

Simulation Using Priority Queues

  • Step 1:
    Sort the list of meetings by their start time.

  • Step 2:
    Initialize two priority queues:
    Free Rooms: Contains all room numbers from 0 to n − 1 (min-heap based on room number).
    Busy Rooms: Contains tuples (end_time, room_number) indicating when a room becomes free.

  • Step 3:
    For each meeting request, first check the busy queue to free up any room whose meeting has ended by the current meeting's start time. Then, if a free room is available, assign the meeting to the free room with the smallest number. Otherwise, pick the busy room that becomes free the earliest, delay the meeting to that room’s available time, and update the meeting’s end time accordingly.

  • Step 4:
    Increase the meeting count for the room that gets assigned the meeting.

  • Step 5:
    After processing all meetings, identify the room with the highest meeting count (if there’s a tie, choose the room with the smallest number).

Complexity Analysis

  • Time Complexity:
    Sorting the meetings takes O(m log m), where m is the number of meetings. Each meeting is processed with heap operations that take O(log n). Thus, the overall time complexity is O(m log m + m log n).

  • Space Complexity:
    The space required is O(n + m) for storing the free rooms, busy rooms, and the meeting list.

Python Code

Python3
Python3

. . . .

Java Code

Java
Java

. . . .

Step-by-Step Walkthrough and Visual Examples

  1. Sort Meetings:
    Meetings are sorted by their start time so that they are processed in chronological order.

  2. Initialize Data Structures:
    • All meeting rooms (0 to n - 1) are initially free; they are stored in a min-heap (free rooms) sorted by room number.
    • A min-heap (busy rooms) tracks rooms currently in use along with their meeting end times.
    • An array is used to count the number of meetings held by each room.

  3. Processing a Meeting:
    For each meeting:

    • Free up any rooms that have completed their meetings by comparing the earliest busy room’s end time with the current meeting’s start time.
    • If a free room is available, assign the meeting immediately.
    • If no free room is available, select the room that becomes available the earliest and delay the meeting accordingly.
    • Update the meeting count for the room and set its new end time.
  4. Final Step:
    After processing all meetings, traverse the meeting count array to determine which room hosted the most meetings. In the case of a tie, the room with the smallest number is chosen.

Common Mistakes

Not Freeing Rooms Correctly:
Ensure that rooms are freed (moved from the busy heap back to the free heap) when their meeting end time is less than or equal to the current meeting’s start time.

Incorrect Delay Calculation:
When no free room is available, the meeting should be delayed to start at the busy room’s end time, not at its original start time.

Tie-breaking for Rooms:
Always choose the room with the smallest number when multiple free rooms are available.

Edge Cases

No Meetings:
If the meetings list is empty, no room is booked and the answer can default to 0.

All Meetings Overlap:
When every meeting overlaps, each meeting will be delayed. The algorithm must correctly update end times based on the delays.

Meetings with the Same Start Time:
If multiple meetings start at the same time, ensure the free room selection and busy room delay logic handles them in order.

Alternative Variations

Multiple Meeting Rooms with Different Priorities:
If rooms had different priorities (e.g., based on amenities or capacity), the free room selection criteria might change.

Minimizing Total Delay:
Instead of counting meetings, one could modify the problem to minimize the total delay incurred by all meetings.

Meeting Rooms II (LeetCode 253):
Find the minimum number of meeting rooms required to schedule all meetings.

TAGS
leetcode
CONTRIBUTOR
Design Gurus Team
-

GET YOUR FREE

Coding Questions Catalog

Design Gurus Newsletter - Latest from our Blog
Boost your coding skills with our essential coding questions catalog.
Take a step towards a better tech career now!
Explore Answers
Related Courses
Grokking the Coding Interview: Patterns for Coding Questions
Grokking the Coding Interview Patterns in Java, Python, JS, C++, C#, and Go. The most comprehensive course with 476 Lessons.
Grokking Modern AI Fundamentals
Master the fundamentals of AI today to lead the tech revolution of tomorrow.
Grokking Data Structures & Algorithms for Coding Interviews
Unlock Coding Interview Success: Dive Deep into Data Structures and Algorithms.
Image
One-Stop Portal For Tech Interviews.
Copyright © 2025 Design Gurus, LLC. All rights reserved.
;