File tree Expand file tree Collapse file tree 1 file changed +34
-0
lines changed Expand file tree Collapse file tree 1 file changed +34
-0
lines changed Original file line number Diff line number Diff line change
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
+ };
You can’t perform that action at this time.
0 commit comments