Ctrl K

Nearest Roomy Enough Room

Hard
A hotel's rooms are listed in rooms: an entry [number, area] is a room with the unique number number and floor area area. Guests book through guests: a request [want, need] means the guest wants a room numbered as close to want as possible, with an area of at least need. Among the rooms with area at least need, pick the one with the smallest |number - want|, breaking ties by the smaller number. If no room is large enough, the answer is -1. Requests are independent: a room picked for one guest stays available to the others. Return the answers in request order.
Example 1
rooms=[[101, 20], [104, 35], [108, 15], [110, 40]], guests=[[105, 30], [109, 10], [107, 38], [100, 50]] → [104, 108, 110, -1]

For the first guest rooms 104 and 110 are big enough, and 104 is closer. For the second every room fits; 108 and 110 are both 1 away, so the smaller number, 108, wins. The third guest can only have 110. No room has an area of 50 or more, hence -1.

Example 2
rooms=[[7, 5]], guests=[[1, 5], [7, 6]] → [7, -1]

The only room is big enough for the first guest however far it is, and too small for the second.

Constraints
  • 1 ≤ len(rooms) ≤ 10⁵
  • 1 ≤ len(guests) ≤ 10⁴
  • 1 ≤ number, want ≤ 10⁷
  • 1 ≤ area, need ≤ 10⁷
  • All numbers are distinct
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsFlowers in Bloom on Visit Day HardDeal Out From the Lowest Card HardClimbing Along Rows and Columns HardOne Edit to the Closest Match MediumClose Readings Nearby Hard
loading Python…
Input
rooms=[[101, 20], [104, 35], [108, 15], [110, 40]], guests=[[105, 30], [109, 10], [107, 38], [100, 50]]
Expected
[104, 108, 110, -1]

Run checks the first 3 tests in your browser. Submit checks all 205 tests on the server, hidden ones included.