Skip to content

Commit 50d7979

Browse files
Merge pull request #1 from gantavya12/patch-1
Create Leetcode-The-Skyline-Problem.cpp
2 parents 38d09c2 + 5dac3ba commit 50d7979

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

CPP/Leetcode-The-Skyline-Problem.cpp

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
class Solution {
2+
public:
3+
vector<vector<int>> getSkyline(vector<vector<int>>& buildings) {
4+
vector<vector<int>> ans;
5+
multiset<int> pq{0};
6+
7+
vector<pair<int, int>> points;
8+
9+
for(auto b: buildings){
10+
points.push_back({b[0], -b[2]});
11+
points.push_back({b[1], b[2]});
12+
}
13+
14+
sort(points.begin(), points.end());
15+
16+
int ongoingHeight = 0;
17+
18+
// points.first = x coordinate, points.second = height
19+
for(int i = 0; i < points.size(); i++){
20+
int currentPoint = points[i].first;
21+
int heightAtCurrentPoint = points[i].second;
22+
23+
if(heightAtCurrentPoint < 0){
24+
pq.insert(-heightAtCurrentPoint);
25+
} else {
26+
pq.erase(pq.find(heightAtCurrentPoint));
27+
}
28+
29+
// after inserting/removing heightAtI, if there's a change
30+
auto pqTop = *pq.rbegin();
31+
if(ongoingHeight != pqTop){
32+
ongoingHeight = pqTop;
33+
ans.push_back({currentPoint, ongoingHeight});
34+
}
35+
}
36+
37+
return ans;
38+
}
39+
};

0 commit comments

Comments
 (0)