1847. Closest Room.cpp
Code\n\nclass Solution {\npublic:\n    vector<int> closestRoom(vector<vector<int>>& rooms, vector<vector<int>>& queries) {\n        for (int i = 0; i < queries.
**Code**\n```\nclass Solution {\npublic:\n    vector<int> closestRoom(vector<vector<int>>& rooms, vector<vector<int>>& queries) {\n        for (int i = 0; i < queries.size(); ++i) queries[i].push_back(i);         \n    sort(rooms.begin(), rooms.end(), [](auto& lhs, auto& rhs){ return lhs[1] > rhs[1]; }); \n        sort(queries.begin(), queries.end(), [](auto& lhs, auto& rhs){ return lhs[1] > rhs[1]; });         \n        set<int> st; \n        vector<int> ans(queries.size(), -1); \n        for (int i = 0, k = 0; i < queries.size(); ++i) {\n            while (k < rooms.size() && rooms[k][1] >= queries[i][1]) \n                st.insert(rooms[k++][0]); \n            if (st.size()) {\n                auto it = st.upper_bound(queries[i][0]); \n                int diff = INT_MAX; \n                if (it != st.end()) diff = *it - queries[i][0]; \n                if (it != st.begin() && queries[i][0] - *prev(it) <= diff) --it; \n                ans[queries[i][2]] = *it; \n            } \n        }\n        return ans; \n    }\n};\n```
['C']