Closest Room Problem
Description
LeetCode Problem 1847.
There is a hotel with n rooms. The rooms are represented by a 2D integer array rooms where rooms[i] = [roomId_i, size_i] denotes that there is a room with room number roomId_i and size equal to size_i. Each roomId_i is guaranteed to be unique.
You are also given k queries in a 2D array queries where queries[j] = [preferred_j, minSize_j]. The answer to the j^th query is the room number id of a room such that:
- The room has a size of at least
minSize_j, and abs(id - preferred_j)is minimized, whereabs(x)is the absolute value ofx.
If there is a tie in the absolute difference, then use the room with the smallest such id. If there is no such room, the answer is -1.
Return an array answer of length k where answer[j] contains the answer to the j^th query.
Example 1:
Input: rooms = [[2,2],[1,2],[3,2]], queries = [[3,1],[3,3],[5,2]]
Output: [3,-1,3]
Explanation: The answers to the queries are as follows:
Query = [3,1]: Room number 3 is the closest as abs(3 - 3) = 0, and its size of 2 is at least 1. The answer is 3.
Query = [3,3]: There are no rooms with a size of at least 3, so the answer is -1.
Query = [5,2]: Room number 3 is the closest as abs(3 - 5) = 2, and its size of 2 is at least 2. The answer is 3.
Example 2:
Input: rooms = [[1,4],[2,3],[3,5],[4,1],[5,2]], queries = [[2,3],[2,4],[2,5]]
Output: [2,1,3]
Explanation: The answers to the queries are as follows:
Query = [2,3]: Room number 2 is the closest as abs(2 - 2) = 0, and its size of 3 is at least 3. The answer is 2.
Query = [2,4]: Room numbers 1 and 3 both have sizes of at least 4. The answer is 1 since it is smaller.
Query = [2,5]: Room number 3 is the only room with a size of at least 5. The answer is 3.
Constraints:
n == rooms.length1 <= n <= 10^5k == queries.length1 <= k <= 10^41 <= roomId_i, preferred_j <= 10^71 <= size_i, minSize_j <= 10^7
Solution
Approach: Offline Queries with a Sorted Set
The order we answer queries in does not matter, so sort the queries by minimum size and handle them from small to large. Sort the rooms by size too.
Keep a sorted set of room ids, initially all rooms. For each query in increasing order of minimum size, first remove every room whose size is below that minimum (they only get too small for later queries as well). Then binary search the set for the id closest to the preferred id, preferring the smaller id on ties. If the set is empty, the answer is -1. Finally put the answers back in the original query order.
Complexity: O(n log n + k log k) time and O(n + k) space, where n is the number of rooms and k the number of queries.
from bisect import bisect_left, insort
class Solution:
def closestRoom(
self, rooms: List[List[int]], queries: List[List[int]]
) -> List[int]:
rooms.sort(key=lambda x: -x[1])
k = len(queries)
idx = sorted(range(k), key=lambda i: -queries[i][1])
ans = [-1] * k
i, n = 0, len(rooms)
ids = [] # sorted ids of rooms that are big enough
for j in idx:
prefer, min_size = queries[j]
while i < n and rooms[i][1] >= min_size:
insort(ids, rooms[i][0])
i += 1
if not ids:
continue
p = bisect_left(ids, prefer)
if p < len(ids):
ans[j] = ids[p]
if p and (ans[j] == -1 or ans[j] - prefer >= prefer - ids[p - 1]):
ans[j] = ids[p - 1]
return ans