• 218. 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_MAX0 < 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], ...]

    Points : skyline mean set of those points who change hight

     1 /*
     2  * Solution 1 Heap -- PriorityQueue
     3  */
     4 public class Solution {
     5     public List<int[]> getSkyline(int[][] buildings) {
     6         List<int[]> list = new LinkedList<int[]>();
     7         List<int[]> height = new LinkedList<int[]>();
     8         
     9         for (int[] b : buildings) {
    10             height.add(new int[]{b[0], -b[2]});
    11             height.add(new int[]{b[1], b[2]});
    12         }
    13         
    14         Collections.sort(height, (a, b) -> {
    15             if (a[0] != b[0])
    16                 return a[0] - b[0]; // small to large
    17             return a[1] - b[1]; // low to high
    18  });
    19         
    20         PriorityQueue<Integer> pq = new PriorityQueue<Integer>((a, b) -> (b - a));
    21         int pre = 0;
    22         pq.offer(0);
    23         
    24         for (int[] h : height) {
    25             if (h[1] < 0) {
    26                 pq.offer(-h[1]);
    27             } else {
    28                 pq.remove(h[1]);
    29             }
    30             int cur = pq.peek();
    31             if (pre != cur) { // change of height point
    32                 list.add(new int[]{h[0], cur});
    33                 pre = cur;
    34             }
    35         }
    36         return list;
    37     }
    38 }
     1 /*
     2  * Solution 2 Divide and Conquer -- merge
     3  */
     4 public class Solution {
     5     public List<int[]> getSkyline(int[][] buildings) {
     6         if (buildings.length == 0)
     7             return new LinkedList<int[]>();
     8         return recurSkyline(buildings, 0, buildings.length - 1);
     9     }
    10     
    11     private LinkedList<int[]> recurSkyline(int[][] buildings, int s, int t) {
    12         if (s == t) {
    13             LinkedList<int[]> ret = new LinkedList<int[]>();
    14             ret.add(new int[]{buildings[s][0], buildings[s][2]}); // left top
    15             ret.add(new int[]{buildings[s][1], 0}); // right, bottom
    16             return ret;
    17         } else {
    18             int mid = (t - s) / 2 + s;
    19             return merge(recurSkyline(buildings, s, mid), recurSkyline(buildings, mid + 1, t));
    20         }
    21     }
    22     
    23     private LinkedList<int[]> merge(LinkedList<int[]> l1, LinkedList<int[]> l2) {
    24         int h = 0;
    25         int x = 0;
    26         int h1 = 0, h2 = 0;
    27         LinkedList<int[]> ret = new LinkedList<int[]>();
    28         
    29         while (!l1.isEmpty() && !l2.isEmpty()) {
    30             if (l1.getFirst()[0] < l2.getFirst()[0]) {
    31                 x = l1.getFirst()[0]; // getFirst, removeFirst, getLast based on LinkedList, List do not have these functions
    32                 h1 = l1.getFirst()[1];
    33                 h = Math.max(h1, h2);
    34                 l1.removeFirst();
    35             } else if (l1.getFirst()[0] > l2.getFirst()[0]) {
    36                 x = l2.getFirst()[0];
    37                 h2 = l2.getFirst()[1];
    38                 h = Math.max(h1, h2);
    39                 l2.removeFirst();
    40             } else {
    41                 x = l1.getFirst()[0];
    42                 h1 = l1.getFirst()[1];
    43                 h2 = l2.getFirst()[1];
    44                 h = Math.max(h1, h2);
    45                 l1.removeFirst();
    46                 l2.removeFirst();
    47             }
    48             
    49             if (ret.size() == 0 || ret.getLast()[1] != h) { // height change point
    50                 ret.add(new int[]{x, h});
    51             }
    52         }
    53         
    54         ret.addAll(l1);
    55         ret.addAll(l2);
    56         return ret;
    57     }
    58 }
  • 相关阅读:
    编程与操作系统
    maven环境快速搭建
    Maven那点事儿(Eclipse版)
    几种简单的负载均衡算法及其Java代码实现
    Java集合中那些类是线程安全的
    自己随手的一些知识点
    设计模式(一)—— 策略模式
    Unity Audio Source Properties
    [转载]Web前端和后端之区分,以及面临的挑战
    TestNG 与 Junit的比较(转)
  • 原文地址:https://www.cnblogs.com/joycelee/p/5468122.html
Copyright © 2020-2023  润新知