Skip to content

Commit d845e11

Browse files
authored
Create skyline.cpp
1 parent 0fd04f1 commit d845e11

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

Solutions/skyline.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
class Solution {
2+
public:
3+
// Solution hint : https://briangordon.github.io/2014/08/the-skyline-problem.html
4+
vector<vector<int>> getSkyline(vector<vector<int>>& buildings) {
5+
set<vector<int>> cp;
6+
for ( auto x : buildings ){
7+
cp.insert({ x[0] , x[1], x[2], 1});
8+
cp.insert({ x[1] , x[0] , x[2] ,0});
9+
}
10+
set< vector<int> > heap;
11+
vector<vector<int>> ans;
12+
for ( auto x : cp){
13+
14+
if ( x[3]) {
15+
heap.insert({ x[2], x[0] , x[1]});
16+
}
17+
else{
18+
heap.erase(heap.find({x[2] , x[1] , x[0]}));
19+
}
20+
if ( !ans.empty() && ans.back()[0] == x[0]){
21+
ans.pop_back();
22+
}
23+
if ( heap.empty()){
24+
ans.push_back({x[0] ,0 });
25+
}
26+
else
27+
{
28+
if ( ans.empty() || ans.back()[1] !=(*heap.rbegin())[0])
29+
ans.push_back({x[0] , (*heap.rbegin())[0]});
30+
}
31+
}
32+
return ans;
33+
}
34+
};

0 commit comments

Comments
 (0)