Python, sort queries and rooms and maintain sorted array of possible ids.
Intuition\n\nFor every query of (preferred_id, size) you need to find the most suitable id which has at least size. To do this, sort all the values of rooms bas
# Intuition\n\nFor every query of `(preferred_id, size)` you need to find the most suitable `id` which has at least `size`. To do this, sort all the values of rooms based on `size` (I sort in decreasing order, but it does not matter).\n\nNow if you sort all the queries (same order by `size` and remember the position) you can iterate over queries and get all the ids which suit `size >= size_q`. Now when you do this you maintain a sorted list of the possible ids (you can do this with sorted containers).\n\nNow when you added everything, you do a binary search to find which id is the closest.\n\n# Complexity\n- Time complexity: $O(n \\log n)$\n- Space complexity: $O(n)$\n\n# Code\n```\nfrom sortedcontainers import SortedList\n\nclass Solution:\n  def closestRoom(self, rooms: List[List[int]], queries: List[List[int]]) -> List[int]:\n    rooms.sort(key=lambda x: -x[1])\n    queries = [(queries[i][0], queries[i][1], i) for i in range(len(queries))]\n    queries.sort(key=lambda x: -x[1])\n\n    list_ids, pos, res = SortedList([]), 0, [-1] * len(queries)\n    for pid, size, i in queries:\n      while pos < len(rooms) and rooms[pos][1] >= size:\n        list_ids.add(rooms[pos][0])\n        pos += 1\n\n      if not list_ids:\n        res[i] = -1\n      else:\n        p = list_ids.bisect_left(pid)\n        if p == 0:\n          res[i] = list_ids[0]\n        elif p == len(list_ids):\n          res[i] = list_ids[-1]\n        else:\n          if abs(list_ids[p - 1] - pid) <= abs(list_ids[p] - pid):\n            res[i] = list_ids[p - 1]\n          else:\n            res[i] = list_ids[p]\n    \n    return res\n```
['Python3']