-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetSkyline.cpp
45 lines (39 loc) · 1.21 KB
/
getSkyline.cpp
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
45
class Solution {
public:
static bool compare(pair<int, int> ev1, pair<int, int> ev2) {
if(ev1.first == ev2.first)
return ev1.second > ev2.second;
return ev1.first < ev2.first;
}
vector<vector<int>> getSkyline(vector<vector<int>>& buildings) {
typedef pair<int, int> Event;
vector<Event> events;
for(auto a: buildings){
events.emplace_back(a[0], a[2]);
events.emplace_back(a[1], -a[2]);
}
sort(events.begin(), events.end(), compare);
vector<vector<int>> ans;
for(const auto& ev: events){
int x = ev.first;
int h = ev.second;
bool enter = h > 0;
h = abs(h);
if(enter){
if(h > maxHeight())
ans.push_back({x, h});
hs_.insert(h);
}else{
hs_.erase(hs_.equal_range(h).first);
if(h > maxHeight())
ans.push_back({x, maxHeight()});
}
}
return ans;
}
private:
multiset<int> hs_;
int maxHeight(){
return hs_.empty() ? 0 : *hs_.rbegin();
}
};