Java | Sorting and TreeSet | T.C O(K*logN)
Approach - \nInput - rooms = [[1,4],[2,3],[3,5],[4,1],[5,2]], queries = [[2,3],[2,4],[2,5]]\n1. Sort rooms in decreasing order of their room size or index 1 i.e
**Approach** - \nInput - rooms = [[1,4],[2,3],[3,5],[4,1],[5,2]], queries = [[2,3],[2,4],[2,5]]\n1. Sort rooms in decreasing order of their room size or index 1 i.e.\n    rooms =    0      1      2      3      4 \n            [[3,5], [1,4], [2,3], [5,2], [4,1]]\n2. Store indices of queries[] in a new list and sort them on basis of decreasing order of sizes in queries[]\n    qIndices =         2      1      0\n    which represent [[2,5], [2,4], [2,3]] in queries[]\n3. Create a Treeset which will store the room ids because they are unique. idx is index which present that upto indices idx-1, all the rooms are added in the bst. idx starts from 0. idx runs over rooms[].\n4. Iterate over the qIndices and store values in prefId and size for corresponding index in given queries[].\n    4.1. Now, for each i iterating qIndices. Run a for loop until idx < rooms.length and rooms[idx][0] >= size.\n    4.2. Keep adding rooms[idx][0] in the bst and increment idx by 1.\n5. Now, use floor() and higher() methods of bst and fill result[list.get(i)] accordingly.\n\nT.C is O(K * logN), where K = queries.length and N = rooms.length\nS.C is O(N)\n```\nclass Solution {\n    public int[] closestRoom(int[][] rooms, int[][] queries) {\n        Arrays.sort(rooms, (a,b)->b[1] - a[1]);\n        // System.out.println(Arrays.deepToString(rooms));\n        int n = queries.length;\n        int[] result = new int[n];\n        // qIndices have indices of queries.\n        List<Integer> qIndices = new ArrayList<>();\n        for(int i = 0; i<n; ++i) qIndices.add(i);\n        Collections.sort(qIndices, (i1,i2)->queries[i2][1] - queries[i1][1]);\n        // System.out.println(qIndices.toString());\n        // bst have room ids simple\n        TreeSet<Integer> bst = new TreeSet<>();\n        int idx = 0, size, prefId;\n        for(int i = 0; i < qIndices.size(); ++i){\n            prefId = queries[qIndices.get(i)][0]; size = queries[qIndices.get(i)][1];\n            while(idx < rooms.length && rooms[idx][1] >= size){\n                bst.add(rooms[idx][0]);\n                ++idx;\n            }\n            Integer floorRoom = bst.floor(prefId);            \n            Integer higherRoom = bst.higher(prefId);\n            if(floorRoom == null && higherRoom == null) result[qIndices.get(i)] = -1;\n            else if(floorRoom != null && higherRoom == null) result[qIndices.get(i)] = floorRoom;\n            else if(floorRoom == null && higherRoom != null) result[qIndices.get(i)] = higherRoom;\n            else if(prefId - floorRoom <= higherRoom - prefId) result[qIndices.get(i)] = floorRoom;\n            else result[qIndices.get(i)] = higherRoom;            \n            // System.out.printf("i:%d, prefId:%d, size:%d, bst:%s, floorRoom:%d, higherRoom:%d, idx:%d\\n", i, prefId, size, bst.toString(),floorRoom, higherRoom, idx);\n        }\n        return result;\n    }\n}\n```
[]