forked from garvit-bhardwaj/Leetcode-Problems-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
The Skyline Problem
39 lines (31 loc) · 1.16 KB
/
The Skyline Problem
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
class Solution {
public:
vector<vector<int>> getSkyline(vector<vector<int>>& buildings) {
vector<vector<int>> ans;
multiset<int> pq{0};
vector<pair<int, int>> 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;
}
};