Skip to content

Practice Closest Room

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Hard#1847
LeetCode Problem

Closest Room

Problem Statement

rooms contains [roomId,size] with unique ids. For each [preferred,minSize] query, select a room with size at least minSize minimizing abs(roomId - preferred). Break distance ties by smaller id. Return the chosen ids in query order, using -1 when no room qualifies.

Source: Closest Room on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
rooms = [[2,2],[1,2],[3,2]], queries = [[3,1],[3,3],[5,2]]
Output
[3,-1,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]

Constraints

n == rooms.length
1 <= n <= 10^5
k == queries.length
1 <= k <= 10^4
1 <= roomIdi, preferredj <= 10^7
1 <= sizei, minSizej <= 10^7

Related LeetCode Problems

Practice more problems similar to Closest Room.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.