解题思路:参考http://blog.csdn.net/qq508618087/article/details/51311778
利用两个multiset进行解决,将起点、终点分别加入到st中,以负号进行区别,同一高度的起点会排在终点之前。然后,按照顺序处理,如果是起点,则将高度加入height中,如果是终点,则删除掉与之对应起点的高度。在加入起点或删除终点对应的高度时,如果最大的高度发生了变话,说明该点对应的此时的最大高度为界限点,需将其加入到ans中。
class Solution {
public:
vector<pair<int, int>> getSkyline(vector<vector<int>>& buildings) {
if(buildings.size()==0) return {};
multiset<pair<int,int>> st;
vector<pair<int,int>> ans;
for(auto bd:buildings){
st.insert(make_pair(bd[0],-bd[2]));
st.insert(make_pair(bd[1],bd[2]));
}
int max=0;
multiset<int> height{0};
for(auto s:st){
if(s.second < 0) height.insert(-s.second);
else{
height.erase(height.find(s.second));
}
if((*height.rbegin()) != max){
ans.push_back(make_pair(s.first,*height.rbegin()));
max = *height.rbegin();
}
}
return ans;
}
};