您的位置:首页 > 其它

LeetCode218:The Skyline Problem

2015-08-04 11:09 323 查看
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).






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_MAX
,
0
 < 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], ...]


Credits:

Special thanks to @stellari for adding this problem, creating these two awesome images and all test cases.

基本的思想是对输入buildings做一个线性扫描,并且只处理它的两个点(即开始点和结束点);
还有一个关键点是要使用一个优先队列(priority_queue)来记录所有处于"alive"状态的buildings(处于alive状态的建筑是指那么在后面的变量过程中可能会被使用到的建筑);
优先队列需要根据高度和结束点x坐标来进行排序(优先队列的队首元素是高度值最大,如果存在高度值相同的点,结束点x坐标大的点位于队首);
对buildings执行迭代操作,找到当前正在处理的点(即可能的拐点),当前正在处理的点要么是下一个building的开始点,要么是优先队列中队首元素的结束点;
如果当前的building的开始点的x坐标大于优先队列中队首元素的结束点x坐标,那么首先处理优先队列中的队首元素:将优先队列中的元素弹出直到它为空或者找到第一个结束点x坐标大于当前处理点的。
否则,如果当前的building的开始点x坐标小于等于优先队列中堆首元素的结束点x坐标,那么处理当前的building:将它的高度和结束点x坐标放到优先队列中;判断高度值是否发生了变化,如果发生了变化,将它放入到结果集中。

对于上面1的图示说明:



这道题整理了一些思路,但可能写的还是不是很明白。不过实在是给写出这种解法的人给跪了。这是咋想出来的啊?



runtime:824ms

class Solution {
public:
    vector<pair<int, int>> getSkyline(vector<vector<int>>& buildings) {
        vector<pair<int, int>> res;
        int cur=0, cur_X, cur_H =-1,  len = buildings.size();
        priority_queue< pair<int, int>> liveBlg; // 第一个元素是高度buindings[k][2],第二个元素是结束点的x坐标buildings[k][1]
        while(cur<len || !liveBlg.empty())
        { 
            // 如果是最开始处理建筑,或者出现建筑物不连续的情况(即对于上面第4个建筑和第3个建筑分开的情况)
            cur_X = liveBlg.empty()? buildings[cur][0]:liveBlg.top().second; // 最高建筑的结束点

            if(cur>=len || buildings[cur][0] > cur_X)
            {
                //将结束时间小于等于最高建筑结束点的哪些建筑物从优先队列中弹出
                while(!liveBlg.empty() && ( liveBlg.top().second <= cur_X) ) liveBlg.pop();
            }
            else
            { 
                //如果当前遍历到的建筑物在最高的建筑物结束之前开始,那么处理当前的建筑物
                cur_X = buildings[cur][0];
                while(cur<len && buildings[cur][0]== cur_X)  // 处理所有在同一点开始的建筑物
                {  // just push them in the queue
                    liveBlg.push(make_pair(buildings[cur][2], buildings[cur][1]));
                    cur++;
                }
            }
            cur_H = liveBlg.empty()?0:liveBlg.top().first; // 输出最顶端的建筑物的高度
            if(res.empty() || (res.back().second != cur_H) ) res.push_back(make_pair(cur_X, cur_H));
        }
        return res;
    }
};


这是原文的英文讲解
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: