• 218.The Skyline Problem


    问题描述:

    A city's skyline is the outer contour of the silhouette formed by all the buildings in that city when viewed from a distance. Now suppose you are given the locations and height of all the buildings as shown on a cityscape photo (Figure A), write a program to output the skyline formed by these buildings collectively (Figure B).

    Buildings Skyline Contour

    The geometric information of each building is represented by a triplet of integers [Li, Ri, Hi], where Li and Ri are the x coordinates of the left and right edge of the ith building, respectively, and Hi is its height. It is guaranteed that 0 ≤ Li, Ri ≤ INT_MAX0 < Hi ≤ INT_MAX, and Ri - Li > 0. You may assume all buildings are perfect rectangles grounded on an absolutely flat surface at height 0.

    For instance, the dimensions of all buildings in Figure A are recorded as: [ [2 9 10], [3 7 15], [5 12 12], [15 20 10], [19 24 8] ] .

    The output is a list of "key points" (red dots in Figure B) in the format of [ [x1,y1], [x2, y2], [x3, y3], ... ] that uniquely defines a skyline. A key point is the left endpoint of a horizontal line segment. Note that the last key point, where the rightmost building ends, is merely used to mark the termination of the skyline, and always has zero height. Also, the ground in between any two adjacent buildings should be considered part of the skyline contour.

    For instance, the skyline in Figure B should be represented as:[ [2 10], [3 15], [7 12], [12 0], [15 10], [20 8], [24, 0] ].

    Notes:

    • The number of buildings in any input list is guaranteed to be in the range [0, 10000].
    • The input list is already sorted in ascending order by the left x position Li.
    • The output list must be sorted by the x position.
    • There must be no consecutive horizontal lines of equal height in the output skyline. For instance, [...[2 3], [4 5], [7 5], [11 5], [12 7]...] is not acceptable; the three lines of height 5 should be merged into one in the final output as such: [...[2 3], [4 5], [12 7], ...]

    解题思路:

    这道题的意思是这样的,给我们一些三元组的序列(Li, Ri, Hi) 分别代表建筑物i的左起始点(Li)和右终结点(Ri)以及建筑物高度(Hi)

    并且建筑物的宽度(Li-Ri)一定大于0.

    让我们找到建筑物轮廓的点,并返回拐点最后以高度为0的点结尾。

    构成建筑物轮廓的点一定是当前x坐标下最大的y,我们可以用堆来维护当前最大的y。

    有一位大神图文并茂的解释了这道题目:Brian Gordon

    又有另一位大神的解法:百草园的博客

    这个解法是这样的:

    首先构成一个新的二元数组,数组内存的是x坐标与y坐标为建筑物的两端:这里使用了pair来进行存储

    值得一提的是,大神这里堆高度也就是y坐标进行了处理来区别当前点是起始点还是结束点:用负数来代表起始点,正数代表结束点。

    将所有点放到新的数组后,对数组根据x坐标进行排序。

    这时从头开始遍历高度数组,并往堆里加入数据,在此之前我们要在堆里插入0,因为要以0结尾。

    使用pre 和 cur来判断是否为拐点。

    遍历数组时:

      若为起点,则向堆中加入该高度。

      若为终点,在堆中找到该高度并删除(定点删除所以我们用multiset来实现)

      cur为当前最高的高度。与pre进行比较:

            若不同则表明拐点出现

            若相同则拐点没有出现。

    代码:

    class Solution {
    public:
        vector<pair<int, int>> getSkyline(vector<vector<int>>& buildings) {
            vector<pair<int, int>> h, ret;
            multiset<int> m;
            int pre = 0;
            int cur = 0;
            for(auto &b : buildings){
                h.push_back({b[0], -b[2]}); //tag point as start
                h.push_back({b[1], b[2]});  //tag point as end
            }
            sort(h.begin(), h.end());
            m.insert(0);
            for(auto &point : h){
                if(point.second < 0)
                    m.insert(-point.second);
                else
                    m.erase(m.find(point.second));
                cur = *m.rbegin();
                if(cur != pre) {
                    ret.push_back({point.first, cur});
                    pre = cur;
                }
            }
            return ret;
        }
    };
  • 相关阅读:
    MDK中编译程序后Program Size详解
    win10快速访问的文件夹无法删除的解决方法
    stm32类型cl、vl、xl、ld、md、hd的含义
    史上最全软件测试工程师常见的面试题总结(四)【多测师_王sir】
    基于PO和单例设计模式用python+selenium进行ui自动化框架设计【多测师】
    经典的Python编程题【多测师_王sir】
    Java中的泛型【多测师_王sir】【软件测试】
    Java设计模式之单例模式、工厂模式、PO模式【多测师_王sir】
    Java+Selenium做UI自动化中@FindBy和@CacheLookup用法【多测师_王sir】
    postman中接口的入参为图片的处理方式【多测师_王sir】
  • 原文地址:https://www.cnblogs.com/yaoyudadudu/p/9127822.html
Copyright © 2020-2023  润新知