[Java] | sorting & TreeSet
\nclass Solution {\n    public int[] closestRoom(int[][] rooms, int[][] queries) {\n        Arrays.sort(rooms,(a,b)->a[1]-b[1]);\n        \n        int[][]Q=new
```\nclass Solution {\n    public int[] closestRoom(int[][] rooms, int[][] queries) {\n        Arrays.sort(rooms,(a,b)->a[1]-b[1]);\n        \n        int[][]Q=new int[queries.length][3];\n        int []res=new int[queries.length];\n        \n        for(int i=0;i<queries.length;i++)\n        {\n            Q[i][0]=queries[i][0];\n            Q[i][1]=queries[i][1];\n            Q[i][2]=i;\n        }\n        Arrays.sort(Q,(a,b)->b[1]-a[1]); //sort according to room size\n        TreeSet<Integer>set=new TreeSet<>();\n        int h_=rooms.length-1;\n        \n        \n        for(int i=0;i<queries.length;i++){\n            int l=0;\n            int h=h_;\n            \n            while(l<=h){\n                int mid=(l+h)>>1;\n                \n                if(rooms[mid][1] < Q[i][1])l=mid+1;\n                else\n                    h=mid-1;\n            }\n            \n            if(l==rooms.length)res[Q[i][2]]=-1;\n            else{\n            while(h_ >= l){\n                set.add(rooms[h_][0]);\n                h_--;\n            }\n            \n            Integer floor=set.floor(Q[i][0]); //prefered rooms id\n            Integer ceil=set.ceiling(Q[i][0]);\n            \n            int minf=Integer.MAX_VALUE;\n            int id=-1;\n            if(floor!=null){\n                minf=Math.min(minf,Q[i][0]-floor);\n                id=floor;\n            }\n            if(ceil !=null && minf > ceil-Q[i][0])\n                id=ceil;\n            \n            res[Q[i][2]]=id;\n            \n        }\n      }\n        \n        return res;\n    }\n}\n```
[]