The Skyline Problem leetcode 详解


思路:

假设柱子是
{ [0,1,4], [0,2,3], [3,5,3] }

每次扫描一条竖线后,对m进行竖线的压入或者弹出m中对应的左半边的操作,之后用cur表示处理完后当前所在的水平线,prev表示上一次天线点所在的水平线
prev初始化是0,这样碰到第一个柱子由于它高度大于0,一定会产生一个结果,prev = cur = 4


class
Solution { public: vector<pair<int, int>> getSkyline(vector<vector<int>>& buildings) { vector<pair<int, int> > h, res; multiset<int> m; int pre = 0, cur = 0; for (auto &a : buildings) { h.push_back({a[0], -a[2]}); h.push_back({a[1], a[2]}); } sort(h.begin(), h.end()); m.insert(0); for (auto &a : h) { if (a.second < 0) m.insert(-a.second); else m.erase(m.find(a.second)); cur = *prev(m.end()); if (cur != pre) { res.push_back({a.first, cur}); pre = cur; } } return res; } };
原文地址:https://www.cnblogs.com/yanchengwang/p/6260283.html