您的位置:首页 > 产品设计 > UI/UE

【Lintcode】Building Outline

2016-02-19 20:31 477 查看
class Solution {
public:
/**
* @param buildings: A list of lists of integers
* @return: Find the outline of those buildings
*/

set<int> loc;

map<int,vector<int>> st,en;

multiset<int> H;

vector<vector<int>> buildingOutline(vector<vector<int>> &buildings) {
// write your code here
vector<vector<int>> ret;
for (auto x : buildings)
{
loc.insert(x[0]);
loc.insert(x[1]);
st[x[0]].push_back(x[2]);
en[x[1]].push_back(x[2]);
}

int H0 = 0;
int X0 = 0;
H.insert(0);
for (auto X1 : loc)
{
auto H1 = *H.rbegin();
if (H1)
{
if (ret.empty() || H1 != ret[ret.size() - 1][2])
ret.push_back(vector<int>{X0,X1,H1});
else ret[ret.size() - 1][1] = X1;
}
for (auto x : st[X1]) H.insert(x);
for (auto x : en[X1]) H.erase(H.find(x));
X0 = X1;
}

return ret;
}
};


特别要注意,multiset删除某个元素的一个副本和所有副本的区别。

H.erase(x) 会删除 x 的所有副本

我不知道wa了一次嘿嘿,当然之前还ce了5,6次。。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: