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:

  • 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 free. The delayed meeting should have the same duration as the original meeting.
  • 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.

Input & Output

Example 1 — Basic Case
$ Input: n = 2, meetings = [[0,10],[1,5],[2,7],[3,4]]
Output: 0
💡 Note: Room 0: gets [0,10], [2,7] (delayed to [10,17]), [3,4] (delayed to [17,18]) = 3 meetings. Room 1: gets [1,5] = 1 meeting. Room 0 has most meetings.
Example 2 — Equal Distribution
$ Input: n = 3, meetings = [[1,20],[2,10],[3,5],[4,9],[6,8]]
Output: 0
💡 Note: Room 0: [1,20], [6,8] (delayed to [20,22]) = 2 meetings. Room 1: [2,10], [4,9] (delayed to [10,15]) = 2 meetings. Room 2: [3,5] = 1 meeting. Tie between rooms 0 and 1, return lowest number 0.
Example 3 — Single Room
$ Input: n = 1, meetings = [[1,5],[2,3],[4,6]]
Output: 0
💡 Note: Only room 0: gets [1,5], [2,3] (delayed to [5,6]), [4,6] (delayed to [6,8]) = 3 meetings. Room 0 is the only choice.

Constraints

  • 1 ≤ n ≤ 100
  • 1 ≤ meetings.length ≤ 105
  • meetings[i].length == 2
  • 0 ≤ starti < endi ≤ 5 × 105
  • All the values of starti are unique

Visualization

Tap to expand
Meeting Rooms III: Schedule Meetings with DelaysInput: n=2, meetings=[[0,10],[1,5],[2,7],[3,4]]Room 0Room 1Scheduling Process:1. [0,10] → Room 0 (available)2. [1,5] → Room 1 (available)3. [2,7] → Room 0 (delayed to [10,17])4. [3,4] → Room 0 (delayed to [17,18])Room 0: 3Room 1: 1Output: Room 0 (held most meetings)Room 0 gets 3 meetings, Room 1 gets 1 meeting
Understanding the Visualization
1
Input
n=2 rooms, meetings=[[0,10],[1,5],[2,7],[3,4]]
2
Process
Schedule to lowest available room, delay if all busy
3
Output
Room 0 holds most meetings (3), return 0
Key Takeaway
🎯 Key Insight: Use priority queues to efficiently track available and occupied rooms, avoiding O(n) scans for each meeting
Asked in
Google 15 Amazon 12 Microsoft 8 Apple 6
23.4K Views
Medium Frequency
~35 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen