LeetCode218 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], ...]

思路:

方案1

因为我们提前知道有多少建筑,所以我们可以为每一个建筑的左右边界分配一个存储空间来纪律其能够到达的最大值,最后输出就可以了。这里注意建筑边界不连续时需要在中间加一个阻隔存储空间,以表征建筑物不连续时存在的空隙。

class Solution {
public:
    vector<pair<int, int>> getSkyline(vector<vector<int>>& buildings) {
        if(buildings.size() == 0){
            return vector<pair<int,int>>();
        }
        set<int> coors;
        for(vector<int> building:buildings){
            coors.insert(building[0]);
            coors.insert(building[1]);
        }
        int pre = *coors.begin();
        for(int coor:coors){
            if(coor-pre>1)
                coors.insert(coor-1);
        }
        
        map<int,int> coor_sub;
        map<int,int> sub_coor;
        int sub = 0;
        for(int coor:coors){
            coor_sub[coor] = sub;
            sub_coor[sub] = coor;
            sub++;
        }
        
        vector<int> sky(coors.size(), 0);
        for(vector<int> building:buildings){
            int left = coor_sub[building[0]];
            int right = coor_sub[building[1]];
            int height = building[2];
            for(int i = left;i<=right;i++){
                sky[i] = max(sky[i],height);
            }
        }

        vector<pair<int,int>> subRes;
        subRes.push_back({coors.size()-1,0});
        for(int i = coors.size()-1;i>0;i--){
            if(sky[i] < sky[i-1])
                subRes.push_back({i-1, sky[i]});
            else if(sky[i] > sky[i-1])
                subRes.push_back({i, sky[i]});
        }
        subRes.push_back({0, sky[0]});

        vector<pair<int,int>> res;
        for(auto it=subRes.rbegin();it!=subRes.rend();it++){
            res.push_back({sub_coor[(*it).first], (*it).second});
        }
        return res;
    }
};

解决方案2:

具体思路请直接看代码

class Solution {
public:
    vector<pair<int, int>> getSkyline(vector<vector<int>>& buildings) {
        vector<pair<int,int>> borders;
        for(vector<int> building:buildings){
            borders.push_back({building[0], -building[2]});
            borders.push_back({building[1], building[2]});
        }

        multiset<int> heights;
        heights.insert(0);
        int preHeight = 0;
        sort(borders.begin(), borders.end());
        vector<pair<int,int>> res;

        for(pair<int,int> border:borders){
            if(border.second < 0){
                heights.insert(-border.second);
            }
            else{
                auto it2 = heights.find(border.second);
                heights.erase(it2);
            }
            if(preHeight != *heights.rbegin()){
                preHeight = *heights.rbegin();
                res.push_back({border.first,preHeight});
            }
        }
        return res;
    }
};

猜你喜欢

转载自blog.csdn.net/m0_37518259/article/details/89392887