clueless coding // TODO: be smarter

LeetCode The Skyline Problem: CPP Solution



TL;DR Code Solution


vector<pair<int, int>> getSkyline(vector<vector<int>>& buildings) {
    map<int, vector<int>> m;
    for(vector<int> v : buildings){
        m[v[0]].push_back(v[2]);
        m[v[1]].push_back(-v[2]);
    }
    multiset<int> s;
    s.insert(0);
    vector<pair<int, int>> res;
    for(auto it = m.begin(); it != m.end(); ++it){
        for(int height : it->second){
            if(height > 0) s.insert(height);
            else s.erase(s.find(-height));
        }
        if(res.empty() || *s.rbegin() != res.back().second) res.push_back({it->first, *s.rbegin()});
    }
    return res;
}