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 - Priority Queue Solution INPUT n = 2 rooms: Room 0 Room 1 meetings array: [0, 10] - start:0 end:10 [1, 5] - start:1 end:5 [2, 7] - start:2 end:7 [3, 4] - start:3 end:4 Timeline: 0 1 2 3 4 5 6 7 8 9 10 ALGORITHM STEPS 1 Sort meetings by start Already sorted: 0,1,2,3 2 Use 2 Priority Queues available + busy rooms 3 Process each meeting Assign lowest free room 4 Track meeting counts count[] for each room Processing Trace: t=0: [0,10] --> Room 0 t=1: [1,5] --> Room 1 t=5: [2,7] waits --> Room 1 (delayed: ends at 10) t=10:[3,4] waits --> Room 0 (delayed: ends at 11) FINAL RESULT Meeting Counts: Room 0: 2 meetings Room 1: 2 meetings Room Timeline: Room 0: [0,10] * Room 1: [1,5] [5,10] Output: 0 Tie-breaker: lowest room Key Insight: Use two min-heaps: one for available rooms (by room number) and one for busy rooms (by end time, then room number). This ensures O(m log n) time complexity where m = meetings. When rooms tie in count, return the lowest numbered room (Room 0 wins with 2 meetings each). TutorialsPoint - Meeting Rooms III | Priority Queue Solution
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