Better Way | 2 TreeMaps | Java | Binary Search | O(N*logM + M*logN)
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity: $$O(N*logM + M*logN)$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: $$O(N)$$\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nimport java.util.*;\n\nclass Solution {\n\n    // Nested class to represent a room\n    class Room {\n        private int id;\n        private int size;\n        private int index; \n\n        // Constructor to initialize room attributes\n        public Room(int id, int size, int index) {\n            this.id = id;\n            this.size = size;\n            this.index = index;\n        }\n\n        // Getter method for room ID\n        public int getId() {\n            return id;\n        }\n\n        // Getter method for room size\n        public int getSize() {\n            return size;\n        }\n\n        // Getter method for room index in the original array\n        public int getIndex() {\n            return index;\n        }\n\n        // Override toString() method for better representation of Room object\n        @Override\n        public String toString() {\n            return "Room{" +\n                    "id=" + id +\n                    ", size=" + size + ", index=" + index +\n                    \'}\';\n        }\n    }\n\n    // Method to find the closest room to each query\n    public int[] closestRoom(int[][] rooms, int[][] queries) {\n        // Comparator based on size and index\n        Comparator<Room> roomComparator = Comparator\n                .comparingInt(Room::getSize) // Compare rooms by size\n                .thenComparingInt(Room::getIndex); // If sizes are equal, compare by index\n\n        // TreeMap sorted by room size and index\n        TreeMap<Room, List<Integer>> sortedSize = new TreeMap<>(roomComparator);\n        // TreeMap to store room IDs mapped to their indices in the original array\n        TreeMap<Integer, Integer> sortedID = new TreeMap<>();\n\n        // Populate sortedSize and sortedID maps\n        for (int i = 0; i < rooms.length; i++) {\n            // Create a Room object for the current room with its attributes\n            Room room = new Room(rooms[i][0], rooms[i][1], i);\n            // Add room to sortedSize with an empty list for query indices\n            sortedSize.put(room, new ArrayList<>());\n            // Add room ID to sortedID mapped to its index in the original array\n            sortedID.put(rooms[i][0], i);\n        }\n\n        // Processing queries and populating sortedSize\n        for (int i = 0; i < queries.length; i++) {\n            int[] query = queries[i];\n            int minRoomSize = query[1];\n            // Find the smallest room greater than or equal to the query\'s minimum size\n            Room foundRoom = sortedSize.ceilingKey(new Room(-1, minRoomSize, 0)); // Initialize index with 0\n            if (foundRoom != null) {\n                // Add query index to the list corresponding to the found room in sortedSize\n                sortedSize.get(foundRoom).add(i);\n            }\n        }\n\n        // Initialize the result array\n        int[] res = new int[queries.length];\n        Arrays.fill(res, -1);\n       \n        // Processing queries and populating result array\n        for (Room room : sortedSize.keySet()) {\n            for (int qIndex : sortedSize.get(room)) {\n                int desiredRoomID = queries[qIndex][0];\n                // Find the room ID just greater or equal to the desired room ID\n                Integer justGreater = sortedID.ceilingKey(desiredRoomID);\n                // Find the room ID just smaller or equal to the desired room ID\n                Integer justSmaller = sortedID.floorKey(desiredRoomID);\n                // Choose the closest room ID based on absolute difference\n                if (justGreater == null) {\n                    res[qIndex] = justSmaller;\n                } else if (justSmaller == null) {\n                    res[qIndex] = justGreater;\n                } else {\n                    if (Math.abs(desiredRoomID - justGreater) < Math.abs(desiredRoomID - justSmaller)) {\n                        res[qIndex] = justGreater;\n                    } else {\n                        res[qIndex] = justSmaller;\n                    }\n                }\n            }\n            // Remove the processed room ID from sortedID\n            sortedID.remove(room.getId());\n        }\n\n        return res;\n    }\n}\n\n\n```
['Java']