C++大楼轮廓

水平面上有 N 座大楼,每座大楼都是矩阵的形状,可以用三个数字表示(start, end, height),分别代表其在x轴上的起点,终点和高度。大楼之间从远处看可能会重叠,求出 N 座大楼的外轮廓线。

外轮廓线的表示方法为若干三元组,每个三元组包含三个数字 (start, end, height),代表这段轮廓的起始位置,终止位置和高度。

Building Outline

例如:

给出三座大楼:

[[1, 3, 3], [2, 4, 4], [5, 6, 1]]

外轮廓线为:

[[1, 2, 3], [2, 4, 4], [5, 6, 1]]



我的想法是:先声明一个vector<vector<int>> maxpernum,maxpernum的元素的格式是:(point, height),表示在某一个点上的最高值为多少,这样只要判断maxpernum当前元素的height是否为零、是否与上一个maxpernum元素的height是否相等,就可以得出当前这一小段结果,将结果再插入一个新的vector<vector<int>> res中并返回就可以了。但是出现了问题:(如图)


若按之前的方法,那么x=2时取4,x=3时取3,那么不管怎么做,最后都只能获得3或者4之中的一个, 而实际上是2。

从而我将maxpernum的元素(point, height)从表示一点上的最大值,改为表示point到point+1的最大值,上图中x=2时就取2-3之间的最大值2。这样就可以了

class Solution {
public:
    /**
     * @param buildings: A list of lists of integers
     * @return: Find the outline of those buildings
     */
    vector<vector<int> > buildingOutline(vector<vector<int> > &buildings) {
        // write your code here
        int buildingslen = buildings.size();
        vector<vector<int>> res;
        if (buildingslen == 0) {
            return res;
        } else {
            vector<vector<int>> maxpernum;
            int right = buildings[0][1], left = buildings[0][0];
            for (int i = 0; i < buildingslen; i++) {
                if (right < buildings[i][1]) {
                    right = buildings[i][1];
                }
                if (left > buildings[i][0]) {
                    left = buildings[i][0];
                }
            }
            for (int j = left; j <= right; j++) {
                vector<int> cur;
                cur.push_back(j);
                cur.push_back(0);
                maxpernum.push_back(cur);
            }
            int maxlen = maxpernum.size();
            for (int k = 0; k < buildingslen; k++) {//找出最大值maxpernum
                for (int m = 0; m < maxlen; m++) {
                    if (maxpernum[m][0] >= buildings[k][0] && maxpernum[m][0] < buildings[k][1]) {
                        if (maxpernum[m][1] < buildings[k][2]) {
                            maxpernum[m][1] = buildings[k][2];
                        }
                        if (maxpernum[m][0] == buildings[k][1]) {
                            break;
                        }
                    }
                }
            }
            int start = left;
            int height = maxpernum[0][1];
            for (int n = 0; n < maxlen; n++) {//根据height是不是和之前的height相同或者height是否为零判断如何操作
                if (height == 0) {
                    start = maxpernum[n][0];
                    height = maxpernum[n][1];
                    continue;
                } else {
                    if (height == maxpernum[n][1]) {
                        continue;
                    } else {
                        vector<int> cur;
                        cur.push_back(start);
                        cur.push_back(maxpernum[n][0]);
                        cur.push_back(height);
                        res.push_back(cur);
                        start = maxpernum[n][0];
                        height = maxpernum[n][1];
                    }
                }
            }
            return res;
        }
    }
};

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值