From 5dac3ba7f49efe647a03acd8f2ef59ef5af01a51 Mon Sep 17 00:00:00 2001 From: gantavya12 <91743053+gantavya12@users.noreply.github.com> Date: Sat, 1 Oct 2022 02:30:56 +0530 Subject: [PATCH] Create Leetcode-The-Skyline-Problem.cpp --- CPP/Leetcode-The-Skyline-Problem.cpp | 39 ++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 CPP/Leetcode-The-Skyline-Problem.cpp 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; + } +};