[C++] Offline query solution with explanation | Priority Queue | O(nlogn)
class Solution {\npublic:\n    vector closestRoom(vector>& rooms, vector>& queries) {\n        priority_queue > room;\n        vector, int> > query;\n        in
class Solution {\npublic:\n    vector<int> closestRoom(vector<vector<int>>& rooms, vector<vector<int>>& queries) {\n        priority_queue<pair<int, int> > room;\n        vector<pair<pair<int, int>, int> > query;\n        int n = queries.size();\n        \n        //Sort the rooms based on room size in descending order\n        for (auto it : rooms) {\n            room.push({it[1], it[0]});\n        }\n        \n        //setting up data-structure which contains the original query index\n        for (int i = 0; i < n; i++) {\n            query.push_back({{queries[i][1], queries[i][0]}, i});\n        }\n        \n        //Sort the queries based on query min-size in descending order\n        sort(query.begin(), query.end(), greater<pair<pair<int, int>, int> > ());\n        set<int> s;\n        \n        vector<int> ans(n, 0);\n        \n        for (int i = 0; i < query.size(); i++) {\n            //We will insert room-id of all rooms that have space more than min space required for current query in the set\n            while (!room.empty()) {\n                if (room.top().first >= query[i].first.first) {\n                    s.insert(room.top().second);\n                    \n                    room.pop();\n                } else break;\n            }\n            \n            //Now set contains all rooms that satisfy the min size condition\n            //find out the nearest room id that exists, could be at lower_bound or --lower_bound\n            auto it = s.lower_bound(query[i].first.second);\n            if (s.empty()) {\n                ans[query[i].second] = -1;\n            } else if (it == s.begin()) {\n                ans[query[i].second] = *it;\n            } else if (it == s.end()){\n                ans[query[i].second] = *(--it);\n            } else {\n                //prevIt points to element before lower_bound\n                auto prevIt = it;\n                prevIt --;\n                \n                if (abs(*it - query[i].first.second) < abs(*prevIt - query[i].first.second)) {\n                    ans[query[i].second] = *(it);\n                } else {\n                    ans[query[i].second] = *(prevIt);\n                }\n            }\n        }\n        \n        return ans;\n    }\n};
[]