• 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;
        }
    };
  • 相关阅读:
    SQL Server 存储过程
    Ajax从服务器端获取数据
    Ajax中Get请求与Post请求的区别
    get请求
    Post请求
    递归案例
    SQL SERVER 和ACCESS的数据导入导出
    OA
    OBS桌面视频直播软件/推流工具使用指南
    基于EasyIPCamera实现的数字网络摄像机IPCamera的模拟器IPC RTSP Simulator
  • 原文地址:https://www.cnblogs.com/yaoyudadudu/p/9127822.html
Copyright © 2020-2023  润新知