题目描述:
主要思路:
扫描线+优先队列。
首先将端点和高度排序,先排左端点,在排右端点。
然后利用优先队列维护当前区间的最大值。
具体细节见代码。
class Solution {
public:
vector<vector<int>> getSkyline(vector<vector<int>>& buildings) {
vector<pair<int,long>> sortbuildings;
for(auto building:buildings)
{
int l=building[0],r=building[1],height=building[2];
sortbuildings.push_back({l,-height});
sortbuildings.push_back({r,height});
}
sort(sortbuildings.begin(),sortbuildings.end());
multiset<int,greater<int>> pq;
pq.insert(0);
vector<vector<int>> ans;
for(auto build:sortbuildings)
{
int x=build.first;
int y=build.second;
int top=*(pq.begin());
if(y<0)
{
y=-y;
if(y>top)
ans.push_back({x,y});
pq.insert(y);
}
else
{
pq.erase(pq.find(y));
int tmp=*(pq.begin());
if(top>tmp)
ans.push_back({x,tmp});
}
}
return ans;
}
};