LeetCode #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,Hi)和(Ri,Hi)两组坐标,然后对所有点的横坐标排序,从而可以依次遍历。另外还需要维持当前最高高度,在遍历到Li时,将对应高度插入,在遍历到Ri时,要把对应的Li删除,如果发现最高高度在插入或者删除之后发生了改变,说明有一个key point。为了区分Li和Ri,可以把Li置为负数,但是不影响它的绝对值,而为了实现以上的方法,我们需要一个可以排序、查找、删除的数据结构,那么multiset最合适,同时还要注意在遍历前,对multiset插入0,这样在最开始的时候最高高度为0。

class Solution {
public:
    vector<pair<int, int>> getSkyline(vector<vector<int>>& buildings) {
        vector<pair<int,int>> v;
        for(int i=0;i<buildings.size();i++)
        {
            v.push_back(pair<int,int>(buildings[i][0],-buildings[i][2]));
            v.push_back(pair<int,int>(buildings[i][1],buildings[i][2]));
        }
        sort(v.begin(),v.end());
        
        vector<pair<int,int>> result;
        multiset<int> s;
        s.insert(0);
        for(int i=0;i<v.size();i++)
        {
            int pre=*s.rbegin();
            if(v[i].second<0) s.insert(-v[i].second);
            else s.erase(s.find(v[i].second));
            int cur=*s.rbegin();
            if(cur!=pre) result.push_back(pair<int,int>(v[i].first,cur));
        }
        return result;
    }
};

猜你喜欢

转载自blog.csdn.net/lawfile/article/details/81176413