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