leetcode:the skyline problem

class Solution
{
public:
    struct node
    {
        int x, heightIndex;
        bool leftOrRight;
        node(int _x, int _heightIndex, bool _leftOrRight):x(_x), heightIndex(_heightIndex), leftOrRight(_leftOrRight) {}
    };
    static bool cmp(const node & a, const node & b)//去掉static报错
    {
        if(a.x != b.x)
            return a.x < b.x;//小于号不能换成减号
        return a.leftOrRight < b.leftOrRight;
    }
    vector<pair<int, int> > getSkyline(vector<vector<int> >& buildings)
    {
        vector<pair<int, int> > res;
        vector<node> edge;
        vector<int> height;
        int i, index;
        for(i = 0; i < buildings.size(); i++)
        {
            index = height.size();
            edge.push_back(node(buildings[i][0], index, false));
            edge.push_back(node(buildings[i][1], index, true));
            height.push_back(buildings[i][2]);
        }
        sort(edge.begin(), edge.end(), cmp);
        multiset<int> active;
        int currentHeight = 0, newHeight, newX;
        for(i = 0; i < edge.size(); i++)
        {
            if(edge[i].leftOrRight == false)
            {
                active.insert(edge[i].heightIndex);
                newHeight = height[edge[i].heightIndex];
                newX = edge[i].x;
                while(i + 1 < edge.size() && edge[i].x == edge[i + 1].x && edge[i + 1].leftOrRight == false)
                {
                    active.insert(edge[i + 1].heightIndex);
                    newHeight = max(newHeight, height[edge[i + 1].heightIndex]);
                    i++;
                }
                if(currentHeight < newHeight)
                {
                    res.push_back(make_pair(newX, newHeight));
                    currentHeight = newHeight;
                }
            }
            else
            {
                active.erase(edge[i].heightIndex);
                newHeight = height[edge[i].heightIndex];
                newX = edge[i].x;
                while(i + 1 < edge.size() && edge[i].x == edge[i + 1].x && edge[i + 1].leftOrRight == true)
                {
                    active.erase(edge[i + 1].heightIndex);
                    newHeight = max(newHeight, height[edge[i + 1].heightIndex]);
                    i++;
                }
                if(currentHeight == newHeight)
                {
                    multiset<int> :: iterator it;
                    int maxHeight = 0;
                    for(it = active.begin(); it != active.end(); it++)
                        maxHeight = max(maxHeight, height[*it]);
                    if(newHeight > maxHeight)
                    {
                        res.push_back(make_pair(newX, maxHeight));
                        currentHeight = maxHeight;
                    }
                }
            }
        }
        return res;
    }
};

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值