Closest Room - Problem

You're managing a luxury hotel booking system where guests have specific room preferences. Your hotel has n rooms, each with a unique room number and size. You need to efficiently handle k guest queries, where each guest specifies their preferred room number and minimum room size requirement.

For each query, find the room that:

  • Has a size of at least the guest's minimum requirement
  • Has a room number closest to their preferred number
  • In case of ties in distance, choose the room with the smaller room number

If no suitable room exists, return -1 for that query.

Example: If rooms are [[2,130],[3,120],[4,110]] and a guest queries [3,125], only room 2 meets the size requirement (130 โ‰ฅ 125), so return 2.

Input & Output

example_1.py โ€” Basic Hotel Query
$ Input: rooms = [[2,130],[3,120],[4,110]], queries = [[3,125]]
โ€บ Output: [2]
๐Ÿ’ก Note: Guest prefers room 3 with minimum size 125. Only room 2 (size 130) meets the requirement. Distance from 3 to 2 is |2-3| = 1.
example_2.py โ€” Multiple Queries
$ Input: rooms = [[1,100],[2,200],[3,150]], queries = [[2,100],[2,180]]
โ€บ Output: [2,2]
๐Ÿ’ก Note: Query 1: Rooms 1,2,3 all meet size 100. Room 2 is exact match for preferred 2. Query 2: Only rooms 2,3 meet size 180. Room 2 (distance 0) is closer than room 3 (distance 1).
example_3.py โ€” No Valid Room
$ Input: rooms = [[1,50],[2,60]], queries = [[3,100]]
โ€บ Output: [-1]
๐Ÿ’ก Note: No room has size โ‰ฅ 100, so return -1 for this query.

Constraints

  • 1 โ‰ค n โ‰ค 105
  • 1 โ‰ค k โ‰ค 104
  • 1 โ‰ค roomIdi, preferredj โ‰ค 107
  • 1 โ‰ค sizei, minSizej โ‰ค 107
  • All room IDs are unique

Visualization

Tap to expand
๐Ÿจ Smart Hotel Room AssignmentStep 1: Sort Rooms by Size[4,110][3,120][2,130]Step 2: Sort Queries by minSize (Descending)[3,125] first[1,110] secondStep 3: Binary Search ProcessQuery [3,125]: Valid rooms = [2] (size โ‰ฅ 125)๐Ÿ” Binary search for closest to 3: Room 2Distance: |2-3| = 1 โœ“Query [1,110]: Valid rooms = [2,3,4]๐Ÿ” Closest to 1: Room 2 (distance 1)๐ŸŽฏ Key Optimization Benefits:โ€ข Process queries in smart order (largest minSize first)โ€ข Maintain sorted valid rooms for O(log n) lookupโ€ข Reuse valid rooms across multiple queriesโ€ข Binary search eliminates linear scanningโšก Complexity Analysis:Brute Force: O(k ร— n) - Check all rooms per queryOptimal: O(n log n + k log k + (n+k) log n)For n=10โต, k=10โด: 10โน โ†’ ~10โถ operations!๐Ÿš€ 1000x faster for large inputs!
Understanding the Visualization
1
Initial Setup
Sort rooms by size and queries by minimum size requirement (descending)
2
Process High Requirements First
Start with queries needing largest rooms, building valid room set incrementally
3
Binary Search Magic
For each query, binary search the sorted valid rooms to find closest room number
4
Optimal Result
Return the best room for each guest with minimal computational overhead
Key Takeaway
๐ŸŽฏ Key Insight: By processing queries in descending order of size requirement and maintaining a sorted set of valid rooms, we transform an O(kร—n) problem into an O(n log n) solution using the power of binary search!
Asked in
Google 45 Amazon 38 Microsoft 32 Meta 28
23.6K Views
Medium-High Frequency
~25 min Avg. Time
847 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen