2402. Meeting Rooms III
Problem
Rooms 0…n − 1 host meetings[i] = [start, end) (all starts distinct). Each meeting takes the free room with the lowest number; if none is free, it waits for the earliest-freed room and keeps its duration, and waiting meetings are served in order of original start. Return the room that hosted the most meetings (ties: lowest number).
Example: n = 2, meetings = [[0,10],[1,5],[2,7],[3,4]] → 0
Constraints: n ≤ 100; up to 10⁵ meetings.
Process meetings by start time with two min-heaps: free rooms by number and busy rooms by (end time, room). Before each meeting, release rooms that have ended. If a room is free, use the lowest-numbered one; otherwise wait for the earliest-ending room (lowest number on ties) and delay the meeting, keeping its duration. Count uses per room.
Complexity: O(m log m + m log n).
class Solution:
def mostBooked(self, n: int, meetings: List[List[int]]) -> int:
free, busy, count = list(range(n)), [], [0] * n
for s, e in sorted(meetings):
while busy and busy[0][0] <= s:
heappush(free, heappop(busy)[1])
if free:
room = heappop(free)
heappush(busy, (e, room))
else:
end, room = heappop(busy)
heappush(busy, (end + e - s, room))
count[room] += 1
return count.index(max(count))
assert Solution().mostBooked(2, [[0, 10], [1, 5], [2, 7], [3, 4]]) == 0
assert Solution().mostBooked(3, [[1, 20], [2, 10], [3, 5], [4, 9], [6, 8]]) == 1
assert Solution().mostBooked(1, [[0, 1]]) == 0
assert Solution().mostBooked(4, [[18, 19], [3, 12], [17, 19], [2, 13], [7, 10]]) == 0
class Solution {
public int mostBooked(int n, int[][] meetings) {
Arrays.sort(meetings, (a, b) -> Integer.compare(a[0], b[0]));
PriorityQueue<Integer> free = new PriorityQueue<>();
for (int i = 0; i < n; i++) free.add(i);
PriorityQueue<long[]> busy = new PriorityQueue<>((a, b) -> a[0] != b[0] ? Long.compare(a[0], b[0]) : Long.compare(a[1], b[1]));
int[] count = new int[n];
for (int[] m : meetings) {
while (!busy.isEmpty() && busy.peek()[0] <= m[0]) free.add((int) busy.poll()[1]);
int room;
if (!free.isEmpty()) { room = free.poll(); busy.add(new long[]{m[1], room}); }
else { long[] b = busy.poll(); room = (int) b[1]; busy.add(new long[]{b[0] + m[1] - m[0], room}); }
count[room]++;
}
int best = 0;
for (int i = 1; i < n; i++) if (count[i] > count[best]) best = i;
return best;
}
}
type IntHeap []int // free rooms, lowest number first
func (h IntHeap) Len() int { return len(h) }
func (h IntHeap) Less(i, j int) bool { return h[i] < h[j] }
func (h IntHeap) Swap(i, j int) { h[i], h[j] = h[j], h[i] }
func (h *IntHeap) Push(x any) { *h = append(*h, x.(int)) }
func (h *IntHeap) Pop() any {
old := *h
x := old[len(old)-1]
*h = old[:len(old)-1]
return x
}
type busyHeap [][2]int // (free-at time, room): earliest first, then the lowest room
func (h busyHeap) Len() int { return len(h) }
func (h busyHeap) Less(i, j int) bool {
return h[i][0] < h[j][0] || h[i][0] == h[j][0] && h[i][1] < h[j][1]
}
func (h busyHeap) Swap(i, j int) { h[i], h[j] = h[j], h[i] }
func (h *busyHeap) Push(x any) { *h = append(*h, x.([2]int)) }
func (h *busyHeap) Pop() any {
old := *h
x := old[len(old)-1]
*h = old[:len(old)-1]
return x
}
func mostBooked(n int, meetings [][]int) int {
sort.Slice(meetings, func(i, j int) bool { return meetings[i][0] < meetings[j][0] })
free, busy, count := &IntHeap{}, &busyHeap{}, make([]int, n)
for r := 0; r < n; r++ {
heap.Push(free, r)
}
for _, m := range meetings {
s, e := m[0], m[1]
for busy.Len() > 0 && (*busy)[0][0] <= s { // release rooms free by s
heap.Push(free, heap.Pop(busy).([2]int)[1])
}
var room int
if free.Len() > 0 {
room = heap.Pop(free).(int)
heap.Push(busy, [2]int{e, room})
} else { // wait for the earliest room; the meeting keeps its duration
top := heap.Pop(busy).([2]int)
room = top[1]
heap.Push(busy, [2]int{top[0] + e - s, room})
}
count[room]++
}
best := 0
for r := range count {
if count[r] > count[best] {
best = r
}
}
return best
}