TC:95% BinarySearch Sorting C++
sort the room by size(reverse), and sort queries by size(reverse)\nfor first query, go through all the element in rooms for which room_size >= query_room_size\n
sort the room by size(reverse), and sort queries by size(reverse)\nfor first query, go through all the element in rooms for which room_size >= query_room_size\nput all those valid size rooms in a sorted list, but you only put the ids in that sorted list\nthen you can do binary search on the sortedIdList to find the largest <= preferredId or smallest>preferredId\n\n```\nclass Solution {\npublic:\n    vector<int> closestRoom(vector<vector<int>>& rooms, vector<vector<int>>& queries) {\n        //same room can be chosen as many time as you want\n\n        sort(rooms.begin(), rooms.end(), [&](auto& a, auto&b){\n            return a[1] > b[1];\n        });\n        map<pair<int, int>, vector<int>> q_i;\n        for (int i = 0; i < queries.size(); i++){\n            q_i[pair<int, int>(queries[i][1], queries[i][0])].push_back(i);\n        }\n\n        int r_i = 0;\n        set<int>ids;\n        vector<int> ans(queries.size(), -1);\n        for(auto curr = q_i.rbegin(); curr != q_i.rend(); curr++){\n            int minSize = curr->first.first; int preferredId = curr->first.second;\n\n            while (r_i < rooms.size() && rooms[r_i][1] >= minSize){\n                ids.insert(rooms[r_i][0]);\n                r_i++;\n            }\n\n            if(ids.size() == 0) continue;\n            \n            auto closest_id = ids.lower_bound(preferredId);//finds the first id not smaller than preferredId\n            int c_ans = 100000000;\n            if (closest_id != ids.end()){\n                c_ans = *closest_id;\n            }\n            if (closest_id!=ids.begin()) closest_id--;\n            if (preferredId - *closest_id <= c_ans - preferredId){\n                c_ans = *closest_id;\n            }\n\n            for (int q_index: curr->second){\n                ans[q_index] = c_ans;\n            }\n\n        }\n\n        return ans;\n    }\n};\n```
['Binary Search', 'Ordered Map', 'Sorting', 'Ordered Set', 'C++']