您的位置:首页 > 其它

Leetcode: The Skyline Problem

2015-12-18 12:27 417 查看
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], ...]








参考了/article/6566256.html

复杂度

时间 O(NlogN) 空间 O(N)

思路

如果按照一个矩形一个矩形来处理将会非常麻烦,我们可以把这些矩形拆成两个点,一个左上顶点,一个右上顶点。将所有顶点按照横坐标排序后,我们开始遍历这些点。遍历时,通过一个堆来得知当前图形的最高位置。堆顶是所有顶点中最高的点,只要这个点没被移出堆,说明这个最高的矩形还没结束。对于左顶点,我们将其加入堆中。对于右顶点,我们找出堆中其相应的左顶点,然后移出这个左顶点,同时也意味这这个矩形的结束。具体代码中,为了在排序后的顶点列表中区分左右顶点,左顶点的值是正数,而右顶点值则存的是负数。

用maxHeap存高度,把楼的上升沿和下降沿都存到一个List<int[]> edges里,edge[0]是横坐标,edge[1]是高度,上升沿高度为正,下降沿高度为负,然后按时间先后顺序重排

理解16行: 如果一个house[2,5,2]另外一个[5,8,3], 第一个House下降沿[5,-2]挨着第二个house上升沿[5,3],结果要求中间是没有gap的,直接是:[[2,2],[5,3],[8,0]]. 而不是[[2,2],[5,0],[5,3],[8,0]]

为了达到这个目的,需要把[5,3]放在[5,-2]之前,这样edge添加顺序是[2,2],[5,3],[5,-2],[8,-3], 到[5,-2]时,删掉heap里的2,但是heap不为空还有3,所以0不会被添加

public int compare(int[] a, int[] b) {
return a[0]==b[0]? b[1]-a[1] : a[0]-b[0];
}


 public class Solution {
public List<int[]> getSkyline(int[][] buildings) {
List<int[]> res = new ArrayList<int[]>();
PriorityQueue<Integer> maxHeap = new PriorityQueue<Integer>(11, new Comparator<Integer>(){
public int compare(Integer a, Integer b) {
return b - a;
}
});
List<int[]> edges = new ArrayList<int[]>();
for (int[] building : buildings) {
edges.add(new int[]{building[0], building[2]});
edges.add(new int[]{building[1], -building[2]});
}
Collections.sort(edges, new Comparator<int[]>() {
public int compare(int[] a, int[] b) { return a[0]==b[0]? b[1]-a[1] : a[0]-b[0]; }
});

int cur=0, pre=0;
for (int[] edge : edges) {
if (edge[1] > 0) {
maxHeap.add(edge[1]);
cur = maxHeap.peek();
}
else {
maxHeap.remove(-edge[1]);
cur = maxHeap.isEmpty()? 0 : maxHeap.peek();
}
if (cur != pre) {
res.add(new int[]{edge[0], cur});
pre = cur;
}
}

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