Best Solution Explained
\n\nclass Solution {\n    public int[] closestRoom(int[][] rooms, int[][] queries) {\n        int n = rooms.length, k = queries.length;\n        Integer[] index
\n```\nclass Solution {\n    public int[] closestRoom(int[][] rooms, int[][] queries) {\n        int n = rooms.length, k = queries.length;\n        Integer[] indexes = new Integer[k];\n        for (int i = 0; i < k; i++) indexes[i] = i;\n        Arrays.sort(rooms, (a, b) -> Integer.compare(b[1], a[1])); //Sort by decreasing order of room size\n        Arrays.sort(indexes, (a, b) -> Integer.compare(queries[b][1], queries[a][1])); // Sort by decreasing order of query minSize\n        TreeSet<Integer> roomIdsSoFar = new TreeSet<>();\n        int[] ans = new int[k];\n        int i = 0;\n        for (int index : indexes) {\n            while (i < n && rooms[i][1] >= queries[index][1]) { // Add id of the room which its size >= query minSize\n                roomIdsSoFar.add(rooms[i++][0]);\n            }\n            ans[index] = searchClosetRoomId(roomIdsSoFar, queries[index][0]);\n        }\n        return ans;\n    }\n    int searchClosetRoomId(TreeSet<Integer> treeSet, int preferredId) {\n        Integer floor = treeSet.floor(preferredId);\n        Integer ceiling = treeSet.ceiling(preferredId);\n        int ansAbs = Integer.MAX_VALUE, ans = -1;\n        if (floor != null) {\n            ans = floor;\n            ansAbs = Math.abs(preferredId - floor);\n        }\n        if (ceiling != null && ansAbs > Math.abs(preferredId - ceiling)) {\n            ans = ceiling;\n        }\n        return ans;\n    }\n}\n```
['Python', 'C++', 'Java', 'Go', 'Python3']