Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Break down a hard problem into reliable checkpoints, edge-case handling, and complexity trade-offs.
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:
Return the number of the room that held the most meetings. If there are multiple rooms, return the room with the lowest number.
A half-closed interval [a, b) is the interval between a and b including a and not including b.
Example 1:
Input: n = 2, meetings = [[0,10],[1,5],[2,7],[3,4]] Output: 0 Explanation: - At time 0, both rooms are not being used. The first meeting starts in room 0. - At time 1, only room 1 is not being used. The second meeting starts in room 1. - At time 2, both rooms are being used. The third meeting is delayed. - At time 3, both rooms are being used. The fourth meeting is delayed. - At time 5, the meeting in room 1 finishes. The third meeting starts in room 1 for the time period [5,10). - At time 10, the meetings in both rooms finish. The fourth meeting starts in room 0 for the time period [10,11). Both rooms 0 and 1 held 2 meetings, so we return 0.
Example 2:
Input: n = 3, meetings = [[1,20],[2,10],[3,5],[4,9],[6,8]] Output: 1 Explanation: - At time 1, all three rooms are not being used. The first meeting starts in room 0. - At time 2, rooms 1 and 2 are not being used. The second meeting starts in room 1. - At time 3, only room 2 is not being used. The third meeting starts in room 2. - At time 4, all three rooms are being used. The fourth meeting is delayed. - At time 5, the meeting in room 2 finishes. The fourth meeting starts in room 2 for the time period [5,10). - At time 6, all three rooms are being used. The fifth meeting is delayed. - At time 10, the meetings in rooms 1 and 2 finish. The fifth meeting starts in room 1 for the time period [10,12). Room 0 held 1 meeting while rooms 1 and 2 each held 2 meetings, so we return 1.
Constraints:
1 <= n <= 1001 <= meetings.length <= 105meetings[i].length == 20 <= starti < endi <= 5 * 105starti are unique.Problem summary: 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. A half-closed interval [a, b) is the interval between a and b including a and not including b.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Hash Map
2 [[0,10],[1,5],[2,7],[3,4]]
3 [[1,20],[2,10],[3,5],[4,9],[6,8]]
meeting-rooms)meeting-rooms-ii)maximum-number-of-events-that-can-be-attended)find-servers-that-handled-most-number-of-requests)maximum-number-of-events-that-can-be-attended-ii)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2402: Meeting Rooms III
class Solution {
public int mostBooked(int n, int[][] meetings) {
Arrays.sort(meetings, (a, b) -> a[0] - b[0]);
PriorityQueue<int[]> busy
= new PriorityQueue<>((a, b) -> a[0] == b[0] ? a[1] - b[1] : a[0] - b[0]);
PriorityQueue<Integer> idle = new PriorityQueue<>();
for (int i = 0; i < n; ++i) {
idle.offer(i);
}
int[] cnt = new int[n];
for (var v : meetings) {
int s = v[0], e = v[1];
while (!busy.isEmpty() && busy.peek()[0] <= s) {
idle.offer(busy.poll()[1]);
}
int i = 0;
if (!idle.isEmpty()) {
i = idle.poll();
busy.offer(new int[] {e, i});
} else {
var x = busy.poll();
i = x[1];
busy.offer(new int[] {x[0] + e - s, i});
}
++cnt[i];
}
int ans = 0;
for (int i = 0; i < n; ++i) {
if (cnt[ans] < cnt[i]) {
ans = i;
}
}
return ans;
}
}
// Accepted solution for LeetCode #2402: Meeting Rooms III
func mostBooked(n int, meetings [][]int) int {
sort.Slice(meetings, func(i, j int) bool { return meetings[i][0] < meetings[j][0] })
idle := hp{make([]int, n)}
for i := 0; i < n; i++ {
idle.IntSlice[i] = i
}
busy := hp2{}
cnt := make([]int, n)
for _, v := range meetings {
s, e := v[0], v[1]
for len(busy) > 0 && busy[0].end <= s {
heap.Push(&idle, heap.Pop(&busy).(pair).i)
}
var i int
if idle.Len() > 0 {
i = heap.Pop(&idle).(int)
heap.Push(&busy, pair{e, i})
} else {
x := heap.Pop(&busy).(pair)
i = x.i
heap.Push(&busy, pair{x.end + e - s, i})
}
cnt[i]++
}
ans := 0
for i, v := range cnt {
if cnt[ans] < v {
ans = i
}
}
return ans
}
type hp struct{ sort.IntSlice }
func (h *hp) Push(v any) { h.IntSlice = append(h.IntSlice, v.(int)) }
func (h *hp) Pop() any {
a := h.IntSlice
v := a[len(a)-1]
h.IntSlice = a[:len(a)-1]
return v
}
type pair struct{ end, i int }
type hp2 []pair
func (h hp2) Len() int { return len(h) }
func (h hp2) Less(i, j int) bool {
a, b := h[i], h[j]
return a.end < b.end || a.end == b.end && a.i < b.i
}
func (h hp2) Swap(i, j int) { h[i], h[j] = h[j], h[i] }
func (h *hp2) Push(v any) { *h = append(*h, v.(pair)) }
func (h *hp2) Pop() any { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1]; return v }
# Accepted solution for LeetCode #2402: Meeting Rooms III
class Solution:
def mostBooked(self, n: int, meetings: List[List[int]]) -> int:
meetings.sort(key=lambda x: x[0])
busy = []
idle = list(range(n))
heapify(idle)
cnt = [0] * n
for s, e in meetings:
while busy and busy[0][0] <= s:
heappush(idle, heappop(busy)[1])
i = 0
if idle:
i = heappop(idle)
heappush(busy, (e, i))
else:
time_end, i = heappop(busy)
heappush(busy, (time_end + e - s, i))
cnt[i] += 1
ans = 0
for i in range(n):
if cnt[ans] < cnt[i]:
ans = i
return ans
// Accepted solution for LeetCode #2402: Meeting Rooms III
// Rust example auto-generated from java reference.
// Replace the signature and local types with the exact LeetCode harness for this problem.
impl Solution {
pub fn rust_example() {
// Port the logic from the reference block below.
}
}
// Reference (java):
// // Accepted solution for LeetCode #2402: Meeting Rooms III
// class Solution {
// public int mostBooked(int n, int[][] meetings) {
// Arrays.sort(meetings, (a, b) -> a[0] - b[0]);
// PriorityQueue<int[]> busy
// = new PriorityQueue<>((a, b) -> a[0] == b[0] ? a[1] - b[1] : a[0] - b[0]);
// PriorityQueue<Integer> idle = new PriorityQueue<>();
// for (int i = 0; i < n; ++i) {
// idle.offer(i);
// }
// int[] cnt = new int[n];
// for (var v : meetings) {
// int s = v[0], e = v[1];
// while (!busy.isEmpty() && busy.peek()[0] <= s) {
// idle.offer(busy.poll()[1]);
// }
// int i = 0;
// if (!idle.isEmpty()) {
// i = idle.poll();
// busy.offer(new int[] {e, i});
// } else {
// var x = busy.poll();
// i = x[1];
// busy.offer(new int[] {x[0] + e - s, i});
// }
// ++cnt[i];
// }
// int ans = 0;
// for (int i = 0; i < n; ++i) {
// if (cnt[ans] < cnt[i]) {
// ans = i;
// }
// }
// return ans;
// }
// }
// Accepted solution for LeetCode #2402: Meeting Rooms III
function mostBooked(n: number, meetings: number[][]): number {
meetings.sort((a, b) => a[0] - b[0]);
const idle = new MinPriorityQueue<number>();
for (let i = 0; i < n; ++i) {
idle.enqueue(i);
}
const busy = new PriorityQueue<[number, number]>((a, b) => {
if (a[0] === b[0]) {
return a[1] - b[1];
}
return a[0] - b[0];
});
const cnt: number[] = new Array(n).fill(0);
for (const v of meetings) {
const s = v[0],
e = v[1];
while (!busy.isEmpty() && busy.front()[0] <= s) {
const i = busy.dequeue()[1];
idle.enqueue(i);
}
let i = 0;
if (!idle.isEmpty()) {
i = idle.dequeue();
busy.enqueue([e, i]);
} else {
const x = busy.dequeue();
i = x[1];
busy.enqueue([x[0] + e - s, i]);
}
++cnt[i];
}
let ans = 0;
for (let i = 0; i < n; ++i) {
if (cnt[ans] < cnt[i]) {
ans = i;
}
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Zero-count keys stay in map and break distinct/count constraints.
Usually fails on: Window/map size checks are consistently off by one.
Fix: Delete keys when count reaches zero.