binary search python code
```\nclass Solution:\n    def closestRoom(self, rooms: List[List[int]], queries: List[List[int]]) -> List[int]:\n        def binary_search(arr, target):\n      
```\nclass Solution:\n    def closestRoom(self, rooms: List[List[int]], queries: List[List[int]]) -> List[int]:\n        def binary_search(arr, target):\n            left, right = 0, len(arr) - 1\n            while left < right:\n                mid = right - (right - left) // 2\n                if arr[mid] <= target:\n                    left = mid\n                else:\n                    right = mid - 1\n            return left\n        \n        # sort rooms and queries\n        rooms.sort(key = lambda x : x[1], reverse= True)\n        new_queries = []\n        for i in range(len(queries)):\n            new_queries.append((queries[i][0], queries[i][1], i))\n        new_queries.sort(key = lambda x : x[1], reverse= True)\n        \n        ans = [-1 for _ in range(len(new_queries))]\n        # find candidates\n        cur_search = 0\n        candidates = []\n        for query in new_queries:\n            queryPrefered, querySize, queryIdx = query\n            #print(query)\n            while cur_search < len(rooms):\n                if rooms[cur_search][1] >= querySize:\n                    room_id = rooms[cur_search][0]\n                    insert_idx = binary_search(candidates, room_id)\n                    if len(candidates) > 0 and candidates[insert_idx] < room_id:\n                        candidates.insert(insert_idx + 1, room_id)\n                    else:\n                        candidates.insert(insert_idx, room_id)\n                    cur_search += 1\n                else:\n                    break\n            #print(candidates)\n            if len(candidates) > 0:\n                # search queryPrefered\n                found_id = binary_search(candidates, queryPrefered)\n                ans[queryIdx] = candidates[found_id]\n                if candidates[found_id] < queryPrefered and found_id < len(candidates) - 1:   \n                     if abs(queryPrefered - candidates[found_id]) > abs(queryPrefered - candidates[found_id + 1]):\n                            ans[queryIdx] = candidates[found_id + 1]\n        return ans
[]