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.length
1 <= n <= 105
k == queries.length
1 <= k <= 104
1 <= roomIdi, preferredj <= 107
1 <= sizei, minSizej <= 107
This problem asks to find the closest room to a preferred room number, given a minimum size constraint. A naive approach would be to iterate through all rooms for each query, but this leads to O(n*k) time complexity, where n is the number of rooms and k is the number of queries. This is inefficient for large datasets. The optimal solution leverages sorting and efficient data structures to achieve a better time complexity.
The key optimization is to process the queries "offline". This means we don't immediately answer each query as it comes. Instead, we sort the queries and rooms based on relevant criteria, enabling efficient processing in a single pass.
Sorting:
rooms
array by size in ascending order. This is crucial because we can process queries in increasing order of minimum size. As we progress through queries, we only need to consider rooms with sizes greater than or equal to the current query's minimum size.queries
indices by their minSize
in ascending order. This allows us to efficiently iterate through the rooms and process queries with progressively larger minimum size requirements.Ordered Set (or Sorted List):
SortedList
in Python or TreeMap
in Java) to store the room IDs. An ordered set maintains its elements in sorted order, allowing for efficient lookups (e.g., finding the closest room ID).Processing Queries:
rooms
array until we find rooms satisfying the minimum size constraint.minSize
from the ordered set. This efficiently maintains the set to only include eligible rooms.bisect_left
or similar functions) to find the closest room ID to the preferred ID. The ordered set guarantees logarithmic time complexity for this search.Handling Ties:
Time Complexity: The sorting steps take O(n log n) and O(k log k) time. Iterating through the rooms and updating the ordered set takes O(n) time in total (as we visit each room at most once). Binary search within the ordered set for each query takes O(k log n) time. Therefore, the overall time complexity is O(n log n + k log k + n + k log n) which simplifies to O(n log n + k log n). In the case where n >> k the dominant terms are O(n log n) (sorting rooms) and O(k log n) (binary searches)
Space Complexity: O(n) for storing the ordered set (at most all room IDs) and O(k) for storing the query results.
The code examples in the original response demonstrate the approach effectively. The choice of language influences some implementation details (e.g., using SortedList
in Python vs. TreeMap
in Java), but the core algorithm remains consistent. Note that the Go example uses a Red-Black tree for better performance than map
for these operations. The other languages' built-in sorted sets/maps provide the necessary capabilities.
Note: To fully appreciate the efficiency, try comparing runtime for large n and k values between this optimized solution and a naive O(n*k) approach. The difference will be substantial.