Easy To Understand Beginner Friendly and Self Explanatory Solution In JAVA
\n\n# Code\n\nclass Solution {\n    public int[] closestRoom(int[][] rooms, int[][] queries) {\n        Arrays.sort(rooms, new Comparator<int[]>(){\n           
\n\n# Code\n```\nclass Solution {\n    public int[] closestRoom(int[][] rooms, int[][] queries) {\n        Arrays.sort(rooms, new Comparator<int[]>(){\n            @Override\n            public int compare(int[] a, int[] b){\n                return a[1]-b[1];\n            }\n        });\n        int[] ans = new int[queries.length];\n        int ptr = 0;\n        for(int[] q:queries){\n            int prefId = q[0];\n            int minSize = q[1];\n            int left = 0, right = rooms.length-1;\n            while(left<=right){\n                int mid = left+(right-left)/2;\n                if(rooms[mid][1]>=minSize) right = mid - 1;\n                else left = mid + 1;\n            }\n            if(left==rooms.length){\n                ans[ptr++] = -1;\n                continue;\n            }\n            int nearestId = Integer.MAX_VALUE;\n            int minDiff = Integer.MAX_VALUE;\n            while(left<rooms.length){\n                if(Math.abs(rooms[left][0]-prefId)<minDiff){\n                    minDiff = Math.abs(rooms[left][0]-prefId);\n                    nearestId = rooms[left][0];\n                }else if(Math.abs(rooms[left][0]-prefId)==minDiff){\n                    nearestId = Math.min(nearestId, rooms[left][0]);\n                }\n                left++;\n            }\n            ans[ptr++] = nearestId;\n        }\n        return ans;\n    }\n}\n```
['Array', 'Binary Search', 'Sorting', 'Java']