Imagine you're managing a busy conference center with n meeting rooms numbered from 0 to n-1. Throughout the day, you receive a list of meeting requests, each with a specific start and end time.

You are given a 2D integer array meetings where meetings[i] = [starti, endi] represents a meeting that needs the time interval [starti, endi) (half-closed interval - includes start time but excludes end time).

Room Allocation Rules:

  • ๐Ÿข Each meeting gets assigned to the lowest-numbered available room
  • โฐ If no rooms are free, the meeting is delayed until a room becomes available (keeping the same duration)
  • ๐Ÿ“… When multiple meetings are waiting, priority goes to the meeting with the earliest original start time

Goal: Return the room number that hosted the most meetings. If there's a tie, return the lowest-numbered room.

Input & Output

example_1.py โ€” Basic Room Assignment
$ Input: n = 2, meetings = [[0,10],[1,5],[2,7],[3,4]]
โ€บ Output: 0
๐Ÿ’ก Note: Room 0: meetings [0,10), [1,5) (delayed to [10,15)), [2,7) (delayed to [15,20)), [3,4) (delayed to [20,21)) - 4 meetings. Room 1: no meetings. Room 0 has the most meetings.
example_2.py โ€” Multiple Rooms Used
$ Input: n = 3, meetings = [[1,20],[2,10],[3,5],[4,9],[6,8]]
โ€บ Output: 1
๐Ÿ’ก Note: Room 0 gets [1,20). Room 1 gets [2,10), [4,9) (delayed to [10,15)), [6,8) (delayed to [15,17)) - 3 meetings. Room 2 gets [3,5). Room 1 has the most meetings.
example_3.py โ€” Tie Breaking
$ Input: n = 4, meetings = [[18,19],[3,12],[17,19],[2,13]]
โ€บ Output: 0
๐Ÿ’ก Note: After sorting by start time: [2,13), [3,12), [17,19), [18,19). Each room gets 1 meeting, so return room 0 (lowest number).

Constraints

  • 1 โ‰ค n โ‰ค 100
  • 1 โ‰ค meetings.length โ‰ค 105
  • meetings[i].length == 2
  • 0 โ‰ค starti < endi โ‰ค 5 ร— 105
  • All start times are unique

Visualization

Tap to expand
Meeting Room SchedulerAvailableRooms Queue012OccupiedRooms QueueMeetingCounters[2, 1, 3]ResultRoom2Timeline Example:Time: 0 โ€”โ€”โ€” 5 โ€”โ€”โ€” 10 โ€”โ€”โ€” 15 โ€”โ€”โ€” 20Meeting A (Room 0)Meeting B (Room 1)Meeting C (Delayed)โ€ข Meetings processed in start-time orderโ€ข Lower room numbers preferredโ€ข Delays preserve meeting duration
Understanding the Visualization
1
Sort Meetings
Process meetings in chronological order by start time
2
Track Availability
Use min-heap to always get the lowest available room number
3
Handle Conflicts
When all rooms busy, delay meeting using earliest finishing room
4
Count and Compare
Track meetings per room, return room with maximum count
Key Takeaway
๐ŸŽฏ Key Insight: Priority queues provide O(log n) efficient room management, making this scheduling problem tractable even for large inputs by maintaining optimal room assignment order.
Asked in
Google 35 Amazon 28 Microsoft 22 Meta 18
28.4K Views
High Frequency
~25 min Avg. Time
890 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