Search
⌘K

Leetcode 1847. Closest Room

For each query (preferred, minSize), find the roomId whose size is at least minSize that minimizes abs(id - preferred) (breaking ties by smaller id), or return -1 if none exist. With n up to 1e5 this is typically solved by sorting rooms and queries by size and maintaining an ordered set (balanced BST / sorted list) of eligible roomIds so you can binary-search the nearest id per query in O(log n).


Question Timeline

See when this question was last asked and where, including any notes left by other candidates.

Comments

Your account is free and you can post anonymously if you choose.