-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path218.cpp
32 lines (31 loc) · 977 Bytes
/
218.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
class Solution {
public:
vector<vector<int>> getSkyline(vector<vector<int>>& buildings) {
map<int, vector<int>> position2heights;
for (auto& building : buildings) {
position2heights[building[0]].push_back(building[2]);
position2heights[building[1]].push_back(-building[2]);
}
multiset<int> st;
st.insert(0);
int current = 0;
vector<vector<int>> res;
for (auto& [position, heights] : position2heights) {
for (auto& height : heights) {
if (height > 0) {
st.insert(height);
}
else {
st.erase(st.find(-height));
}
}
int next = *st.rbegin();
if (next == current) continue;
else {
current = next;
res.push_back({position, current});
}
}
return res;
}
};