218. The Skyline Problem

class Solution {
public:
    vector<pair<int, int>> getSkyline(vector<vector<int>>& buildings) {
        vector<pair<int,int>> v;
        for (const auto& b : buildings) {
            v.push_back({b[0], -b[2]});
            v.push_back({b[1], b[2]});
        }
        sort(v.begin(), v.end());
        
        multiset<int> q;
        q.insert(0);
        
        vector<pair<int, int>> res;
        for (const auto& p : v) {
            int pre = -*(q.begin());
            if (p.second > 0) {
                auto it = q.find(-p.second);
                q.erase(it);
            }
            else {
                q.insert(p.second);
            }
            int cur = -*(q.begin());
            if (pre != cur) {
                res.push_back( {p.first, cur} );
            }
        }
        return res;
    }
};

猜你喜欢

转载自www.cnblogs.com/JTechRoad/p/9069380.html