首页 > 其他 > 详细

Lintcode Building Outline

时间:2016-02-19 21:57:54      阅读:386      评论:0      收藏:0      [点我收藏+]
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次。。

Lintcode Building Outline

原文:http://www.cnblogs.com/soya/p/5202115.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!