#2402
Hard Algorithms Meeting rooms iii
Array Hash Table Sorting Heap (Priority Queue) Simulation
51.6% acceptance
Feb 25, 2026
2474
141
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.
Solution
Rust
Time O(n²)
Space O(n)
use std::collections::BinaryHeap;
use std::cmp::Reverse;
impl Solution {
pub fn most_booked(n: i32, mut meetings: Vec<Vec<i32>>) -> i32 {
let n = n as usize;
meetings.sort();
let mut free: BinaryHeap<Reverse<usize>> = (0..n).map(Reverse).collect();
let mut busy: BinaryHeap<Reverse<(i64, usize)>> = BinaryHeap::new();
let mut count = vec![0i32; n];
for m in meetings {
let start = m[0] as i64;
let end = m[1] as i64;
while let Some(&Reverse((et, room))) = busy.peek() {
if et <= start {
busy.pop();
free.push(Reverse(room));
} else {
break;
}
}
if !free.is_empty() {
let Reverse(room) = free.pop().unwrap();
count[room] += 1;
busy.push(Reverse((end, room)));
} else {
let Reverse((et, room)) = busy.pop().unwrap();
count[room] += 1;
busy.push(Reverse((et + end - start, room)));
}
}
let max_count = *count.iter().max().unwrap();
count.iter().position(|&c| c == max_count).unwrap() as i32
}
}