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], ...]
参考了http://www.cnblogs.com/tonyluis/p/4564617.html
复杂度
Time Complexity is O(n^2) because removal in a PQ is O(n), 空间 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]; }
1 public class Solution { 2 public List<int[]> getSkyline(int[][] buildings) { 3 List<int[]> res = new ArrayList<int[]>(); 4 PriorityQueue<Integer> maxHeap = new PriorityQueue<Integer>(11, new Comparator<Integer>(){ 5 public int compare(Integer a, Integer b) { 6 return b - a; 7 } 8 }); 9 List<int[]> edges = new ArrayList<int[]>(); 10 for (int[] building : buildings) { 11 edges.add(new int[]{building[0], building[2]}); 12 edges.add(new int[]{building[1], -building[2]}); 13 } 14 Collections.sort(edges, new Comparator<int[]>() { 15 public int compare(int[] a, int[] b) { 16 return a[0]==b[0]? b[1]-a[1] : a[0]-b[0]; 17 } 18 }); 19 20 int cur=0, pre=0; 21 for (int[] edge : edges) { 22 if (edge[1] > 0) { 23 maxHeap.add(edge[1]); 24 cur = maxHeap.peek(); 25 } 26 else { 27 maxHeap.remove(-edge[1]); 28 cur = maxHeap.isEmpty()? 0 : maxHeap.peek(); 29 } 30 if (cur != pre) { 31 res.add(new int[]{edge[0], cur}); 32 pre = cur; 33 } 34 } 35 36 return res; 37 } 38 }