diff --git a/CPP/Leetcode-The-Skyline-Problem.cpp b/CPP/Leetcode-The-Skyline-Problem.cpp new file mode 100644 index 00000000..70ce4850 --- /dev/null +++ b/CPP/Leetcode-The-Skyline-Problem.cpp @@ -0,0 +1,39 @@ +class Solution { +public: + vector> getSkyline(vector>& buildings) { + vector> ans; + multiset pq{0}; + + vector> points; + + for(auto b: buildings){ + points.push_back({b[0], -b[2]}); + points.push_back({b[1], b[2]}); + } + + sort(points.begin(), points.end()); + + int ongoingHeight = 0; + + // points.first = x coordinate, points.second = height + for(int i = 0; i < points.size(); i++){ + int currentPoint = points[i].first; + int heightAtCurrentPoint = points[i].second; + + if(heightAtCurrentPoint < 0){ + pq.insert(-heightAtCurrentPoint); + } else { + pq.erase(pq.find(heightAtCurrentPoint)); + } + + // after inserting/removing heightAtI, if there's a change + auto pqTop = *pq.rbegin(); + if(ongoingHeight != pqTop){ + ongoingHeight = pqTop; + ans.push_back({currentPoint, ongoingHeight}); + } + } + + return ans; + } +};