Self-explanatory c++ solution using maps


  • 0
    F
    vector<pair<int, int>> getSkyline(vector<vector<int>>& buildings) {
    		map<int, vector<int>> points;
    		for (auto& v : buildings) {
    			int l = v[0], r = v[1], z = v[2];
    			points[l].push_back(z);
    			points[r].push_back(-z);
    		}
    		int prev = 0;
    		vector<pair<int, int>> vp;
    		map<int, int> height { {0, 1} };
    		for (auto& b : points) {
    			int idx = b.first;
    			for (auto h : b.second) {
    				if (h > 0) height[h]++;
    				else if (--height[-h] == 0) height.erase(-h);
    			}
    			int curr = (--height.end())->first;
    			if (curr != prev) {
    				vp.push_back({ idx, curr });
    				prev = curr;
    			}
    		}
    		return vp;
    	}
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.