The Skyline Problem
Sweep Line, Heap, Segment Tree, Binary Indexed Tree
Hard
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], whereLiandRiare the x coordinates of the left and right edge of the ith building, respectively, andHiis its height. It is guaranteed that0 ≤ Li, Ri ≤ INT_MAX,0 < Hi ≤ INT_MAX, andRi - 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], ...]
Solution & Analysis
Sweep Line + Priority Queue
思路
先将buildings[]转化为一系列 critcal points,并且根据点的坐标以及高度排序。一个trick是,将起始点对应的高度设成负值,而终止点的高度设为正值。当然也可以多设定一个位,用来区分起始点和终止点。
再遍历每一个点,并且用PriorityQueue来保存当前的点所对应的建筑的高度,并且每次选取最高的那一个(利用PriorityQueue的排序特性),加入结果列表中即可。
对于那个把起始点高度变成负数,还有一个很巧妙的原因,就是假设如果有三个大楼宽度都一样,叠在一起,只是高度不一样的话,起始点要从大往小读,这样才能保证不会产生三个critical points,而对于结束点,要从小往大读,不然还是会产生三个critical points。把起始点高度变成负数再排序,就很好的解决了顺序这个问题。
pq.remove()是O(n) time, 因此相比之下用TreeMap可以获得O(logn)的remove()时间复杂度
158 ms
Sweep Line + TreeMap (Time complexity improved compare to Priority Queue)
Linked List
2 ms
Segment Tree
34 ms
Divide and Conquer
5 ms, faster than 99.22%, 42.8 MB, less than 89.42%
From GeeksforGeeks: https://www.geeksforgeeks.org/the-skyline-problem-using-divide-and-conquer-algorithm/
We can find Skyline in Θ(nLogn) time using Divide and Conquer. The idea is similar to Merge Sort, divide the given set of buildings in two subsets. Recursively construct skyline for two halves and finally merge the two skylines.
Time complexity of above recursive implementation is same as Merge Sort. T(n) = T(n/2) + Θ(n) Solution of above recurrence is Θ(nLogn)
How to Merge two Skylines?
The idea is similar to merge of merge sort, start from first strips of two skylines, compare x coordinates. Pick the strip with smaller x coordinate and add it to result. The height of added strip is considered as maximum of current heights from skyline1 and skyline2.
Divide and Conquer 6ms
Reference
Last updated
Was this helpful?

