-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path218.cpp
More file actions
44 lines (35 loc) · 1.32 KB
/
218.cpp
File metadata and controls
44 lines (35 loc) · 1.32 KB
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
40
41
42
43
44
class Solution {
public:
vector<vector<int>> getSkyline(vector<vector<int>>& buildings) { //O(n log n)
vector<vector<int>> sol;
multiset<int> hs; hs.insert(0);
priority_queue<pair<int,int>> ends;
int i = 0;
while(i < buildings.size()){
int l = buildings[i][0]; int r = buildings[i][1];
int maxh = buildings[i][2];
int prev_maxh = *(--hs.end());
while(i < buildings.size() && buildings[i][0] == l){
int r = buildings[i][1]; int h = buildings[i][2];
hs.insert(h);
maxh = max(maxh, h);
ends.push({-r, -h});
i++;
}
i--;
if(maxh > prev_maxh){
sol.push_back({l, maxh});
}
while(!ends.empty() && (i == buildings.size()-1 || -ends.top().first < buildings[i+1][0])){
pair<int, int> p = ends.top(); ends.pop();
int rend = -p.first; int hend = -p.second;
hs.erase(hs.find(hend));
if(*(--hs.end()) < hend){
sol.push_back({rend, *(--hs.end())});
}
}
i++;
}
return sol;
}
};