There is a hotel with n rooms. The rooms are represented by a 2D integer array rooms where rooms[i] = [roomIdi, sizei] denotes that there is a room with room number roomIdi and size equal to sizei. Each roomIdi is guaranteed to be unique.
You are also given k queries in a 2D array queries where queries[j] = [preferredj, minSizej]. The answer to the jth query is the room number id of a room such that:
minSizej, andabs(id - preferredj) is minimized, where abs(x) is the absolute value of x.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 jth 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 <= 105k == queries.length1 <= k <= 1041 <= roomIdi, preferredj <= 1071 <= sizei, minSizej <= 107When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
Imagine searching for a room that best fits your size requirement and is as close as possible to a desired room number. The brute force method is like checking every single room to find the best one. We go through each room individually and compare it to our requirements.
Here's how the algorithm would work step-by-step:
def find_closest_room_brute_force(rooms, query_room_number, minimum_size):
closest_room = -1
min_difference = float('inf')
for room_number, room_size in rooms:
# Filter out rooms that don't meet the minimum size requirement
if room_size < minimum_size:
continue
# Calculate the difference between the room number and the target
difference = abs(room_number - query_room_number)
# If we find a closer room or an equally close room, update closest_room
if difference < min_difference:
min_difference = difference
closest_room = room_number
elif difference == min_difference and room_number < closest_room:
# Prefer smaller room numbers if distances are equal
closest_room = room_number
return closest_roomThe optimal strategy solves this problem efficiently by first organizing the available rooms and then quickly finding the best room for each query. We can use organization and intelligent searching to avoid checking every room for every query.
Here's how the algorithm would work step-by-step:
def closest_room(rooms, queries):
room_size_index = 1
query_preferred_id_index = 0
query_minimum_size_index = 1
# Sort rooms based on their size, largest to smallest.
rooms.sort(key=lambda x: x[room_size_index], reverse=True)
indexed_queries = []
for index, query in enumerate(queries):
indexed_queries.append((query[query_preferred_id_index], query[query_minimum_size_index], index))
# Sort queries based on minimum size, largest to smallest.
indexed_queries.sort(key=lambda x: x[1], reverse=True)
number_of_rooms = len(rooms)
number_of_queries = len(queries)
result = [0] * number_of_queries
available_rooms = []
room_id_index = 0
current_room_index = 0
# Process each query in order of decreasing minimum size.
for preferred_id, minimum_size, query_index in indexed_queries:
# Add rooms that meet the minimum size requirement
while current_room_index < number_of_rooms and rooms[current_room_index][room_size_index] >= minimum_size:
available_rooms.append(rooms[current_room_index][room_id_index])
current_room_index += 1
if not available_rooms:
result[query_index] = -1
continue
# Find the closest room to the preferred ID
closest_id = -1
min_difference = float('inf')
for room_id in available_rooms:
difference = abs(room_id - preferred_id)
if difference < min_difference:
min_difference = difference
closest_id = room_id
elif difference == min_difference:
closest_id = min(closest_id, room_id)
result[query_index] = closest_id
return result| Case | How to Handle |
|---|---|
| Empty rooms array | Return an empty list of results since no rooms exist to query. |
| Empty queries array | Return an empty list of results since there are no queries to process. |
| Single room and single query | Check if the room size meets the minimum size and return the room ID if it does, otherwise return -1. |
| Multiple rooms with the same size. | Iterate through all suitable rooms, keeping track of the closest ID to the preferred ID, returning the best one. |
| Room IDs or query IDs are negative. | Handle negative IDs correctly by ensuring they don't cause issues with indexing or other operations (e.g., use a data structure that supports negative keys). |
| Room sizes or query minimum sizes are zero. | Ensure zero size is a valid condition and handled appropriately in the filtering logic. |
| No room meets the minimum size requirement for a query. | Return -1 as the closest room ID when no room satisfies the minimum size. |
| Integer overflow when calculating differences between preferred ID and room IDs. | Use a data type with sufficient range (e.g., long) for calculations to prevent overflow. |