Locate the Lowest-Indexed Room That Can Fit a Meeting
Greedy & IntervalsAuto-graded · 19 test casesLast updated
Tests interval overlap checks under half-open semantics and using binary search on sorted, disjoint bookings to find a free room quickly.
You manage several rooms. Each room has a schedule of existing bookings. Within a room, the bookings are sorted by start time and never overlap. Every booking is a half-open interval [start, end), so a booking covers times start up to but not including end.
You are also given a new meeting request meeting = [s, e], also half-open. Find the lowest room index where this meeting can be added without overlapping any existing booking in that room. If no room can accommodate the meeting, return -1.
Two half-open intervals overlap only when they share at least one moment in time. A meeting that starts exactly when another one ends (or ends exactly when another one starts) does not overlap it.
Constraints:
0 <= len(rooms) <= 10^4- Each room's bookings are sorted by start time and are pairwise non-overlapping.
- The total number of bookings across all rooms is at most
10^5. meetinghas length 2 withs < e.- All times are integers in the range
0to10^9. - Aim for a solution faster than checking every booking in every room.
Examples
Example 1
Input: find_open_room([[[1, 3], [5, 7]], [[2, 4], [8, 10]]], [3, 5])
Output: 0
Hints
Hint 1
Two half-open intervals [a, b) and [s, e) collide exactly when a < e and s < b. Make sure your check treats touching endpoints as non-colliding.
Hint 2
In a sorted, disjoint list, use binary search to find the first booking whose start is at least s. Only the booking just before it and the one at that position can possibly collide with the meeting.
Hint 3
Iterate rooms in index order and return the first index that passes the check. Avoid building a separate list of start times for each room, since that costs linear time per room.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
For each room, the meeting [s, e) fits exactly when no booking collides with it. Because the room's bookings are sorted by start and do not overlap, we only need to look at two neighbors around the insertion point for s:
- Find
idx, the first booking whose start is>= s. Binary search does this in O(log K). - The booking at
idx - 1starts befores. Since bookings are disjoint and sorted, every earlier booking ends no later than the start ofidx - 1, which is befores. So onlyidx - 1can reach pasts. It collides if its end is greater thans. - The booking at
idxstarts at or afters. Every later booking starts even later. So if the booking atidxstarts at or aftere, nothing fromidxonward can collide. If it starts beforee, it collides.
Rooms are checked in index order, and the first room that passes is returned. If none passes, the answer is -1.
Why it is correct
Half-open intervals [a, b) and [s, e) overlap exactly when a < e and s < b. The two checks above implement precisely this condition, restricted to the only two bookings that can possibly overlap. Touching endpoints are handled correctly: a previous booking ending at s gives end > s false, and a next booking starting at e gives start < e false.
Complexity
- Time: O(R · log K), where R is the number of rooms and K is the largest number of bookings in a room. The naive approach that scans every booking is O(R · K).
- Space: O(1) extra. The binary search runs directly on the input lists. Building a list of start times per room would cost O(K) per room and is avoided.
Edge cases
- Empty room:
idx = 0and there are no bookings, so the meeting fits. - Meeting before all bookings:
idx = 0, and the first booking starts at or aftere, so it fits. - Meeting after all bookings:
idx = K, and the last booking must end at or befores. The first check covers this. - Touching boundaries: allowed under half-open semantics.
- Meeting that fully contains a booking: the booking at
idxoridx - 1will collide and the room is rejected. - No rooms: returns
-1.
Solution
from bisect import bisect_left
from typing import List
def find_open_room(rooms: List[List[List[int]]], meeting: List[int]) -> int:
"""Return the lowest index of a room where `meeting` = [s, e) fits without overlap.
rooms: list of rooms; each room is a list of [start, end) bookings sorted by
start and pairwise non-overlapping.
meeting: [s, e] with s < e.
Returns -1 if no room can take the meeting.
"""
s, e = meeting[0], meeting[1]
for room_id, bookings in enumerate(rooms):
# Find the first booking whose start is >= s, without copying a list of starts.
lo, hi = 0, len(bookings)
while lo < hi:
mid = (lo + hi) // 2
if bookings[mid][0] < s:
lo = mid + 1
else:
hi = mid
idx = lo
# The booking just before idx starts before s; it collides if it ends after s.
if idx > 0 and bookings[idx - 1][1] > s:
continue
# The booking at idx starts at or after s; it collides if it starts before e.
if idx < len(bookings) and bookings[idx][0] < e:
continue
return room_id
return -1