Easy C++ , Set and Sorting
Intuition\nSort by weight and put in set\n\n# Approach\nIf current is current then room weight then push rooom weight\n\n# Complexity\n- Time complexity:\nO(n L
# Intuition\nSort by weight and put in set\n\n# Approach\nIf current is current then room weight then push rooom weight\n\n# Complexity\n- Time complexity:\n$$O(n Log (n))$$\n\n- Space complexity:\n$$O(n)$$\n\n# Code\n```\nclass Solution {\npublic:\n\n    static bool compare(vector<int> &a,vector<int> &b)\n    {\n        return a[1]>b[1];\n    }\n\n    vector<int> closestRoom(vector<vector<int>>& room, vector<vector<int>>& query) \n    {\n        int n=query.size();\n\n        for(int i=0;i<n;i++)\n            query[i].push_back(i);\n\n        sort(room.begin(),room.end(),compare);\n        sort(query.begin(),query.end(),compare);\n\n        set<int> st;\n        int j=0;\n\n        room.push_back({INT_MIN,INT_MIN});\n        vector<int> res(n,-1);\n\n        for(auto &q:query)\n        {\n            while(q[1]<=room[j][1])\n            {\n                st.insert(room[j][0]);\n                j++;\n            }\n\n            int l = -1;\n            int r = -1;\n\n            auto it=(st.lower_bound(q[0]));\n            it = it == st.end() && st.size() ? prev(it) : it;\n\n            if(it!=st.end())\n            {\n                l = r =*it;\n\n                if(q[0]<*it)\n                {\n                    it=prev(it);\n                    l=it ==st.end()?l:*it;\n                }else  if(next(it)!=st.end())\n                    r=*next(it);\n                \n            }\n\n            res[q[2]] = abs(l-q[0])<=abs(r-q[0])?l:r;\n        }\n\n        return res;\n    }\n};\n```
['C++']