• Leetcode: 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).
    
    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], ...]

    Buildings Skyline Contour

    参考了http://www.cnblogs.com/tonyluis/p/4564617.html

    复杂度

    Time Complexity is O(n^2) because removal in a PQ is O(n),  空间 O(N)

    I think if replace PQ with TreeMap, the time complexity would be O(nlogn)

    思路

    Use maxHeap to store heights, and it's ok that multiple same height be added at the same time. Heap.remove() would only remove one instance of the height 

    用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<List<Integer>> getSkyline(int[][] buildings) {
     3         List<List<Integer>> res = new ArrayList<>();
     4         
     5         // use rise edge and decline edge to represent a building
     6         List<int[]> edges = new ArrayList<>();
     7         for (int[] building : buildings) {
     8             edges.add(new int[]{building[0], building[2]});
     9             edges.add(new int[]{building[1], -building[2]});
    10         }
    11         Collections.sort(edges, new Comparator<int[]>() {
    12             public int compare(int[] a, int[] b) {
    13                 return a[0]==b[0]? b[1]-a[1] : a[0]-b[0];
    14             }
    15         });
    16         
    17         // define a heap to store height
    18         PriorityQueue<Integer> maxHeap = new PriorityQueue<>((a, b) -> Integer.compare(b, a));
    19         int cur=0, pre=0;
    20         for (int[] edge : edges) {
    21             if (edge[1] > 0) {
    22                 maxHeap.add(edge[1]);
    23                 cur = maxHeap.peek();
    24             }
    25             else {
    26                 maxHeap.remove(-edge[1]);
    27                 cur = maxHeap.isEmpty()? 0 : maxHeap.peek();
    28             }
    29             if (cur != pre) {
    30                 List<Integer> keyPoint = Arrays.asList(new Integer[]{edge[0], cur});
    31                 res.add(keyPoint);
    32                 pre = cur;
    33             }
    34         }
    35         
    36         return res;
    37     }
    38 }
  • 相关阅读:
    Python-HTML基础
    异常处理
    反射hasattr; getattr; setattr; delattr
    Python 属性方法、类方法、静态方法、 特殊属性__doc__ (内建属性)
    Python3 day6面向对象
    re模块计算器作业
    re正则表达式:import re ;re.search()
    hashlib模块学习:hmac
    ConfigParser模块,主要应用于对php.ini等格式的配置文件内容读取和生成。删改较少用
    ymal文档格式 处理
  • 原文地址:https://www.cnblogs.com/EdwardLiu/p/5056684.html
Copyright © 2020-2023  润新知