|
| 1 | +/** |
| 2 | +* A city's skyline is the outer contour of the silhouette formed by all the |
| 3 | +* buildings in that city when viewed from a distance. Now suppose you are |
| 4 | +* given the locations and height of all the buildings as shown on a cityscape |
| 5 | +* photo (Figure A), write a program to output the skyline formed by these |
| 6 | +* buildings collectively (Figure B). |
| 7 | +* |
| 8 | +* https://leetcode.com/static/images/problemset/skyline2.jpg |
| 9 | +* |
| 10 | +* The geometric information of each building is represented by a triplet of |
| 11 | +* integers [Li, Ri, Hi], where Li and Ri are the x coordinates of the left |
| 12 | +* and right edge of the ith building, respectively, and Hi is its height. It |
| 13 | +* is guaranteed that 0 ≤ Li, Ri ≤ INT_MAX, 0 < Hi ≤ INT_MAX, and Ri - Li > 0. |
| 14 | +* You may assume all buildings are perfect rectangles grounded on an |
| 15 | +* absolutely flat surface at height 0. |
| 16 | +* |
| 17 | +* For instance, the dimensions of all buildings in Figure A are recorded as: |
| 18 | +* [ [2 9 10], [3 7 15], [5 12 12], [15 20 10], [19 24 8] ] . |
| 19 | +* |
| 20 | +* The output is a list of "key points" (red dots in Figure B) in the format |
| 21 | +* of [ [x1,y1], [x2, y2], [x3, y3], ... ] that uniquely defines a skyline. A |
| 22 | +* key point is the left endpoint of a horizontal line segment. Note that the |
| 23 | +* last key point, where the rightmost building ends, is merely used to mark |
| 24 | +* the termination of the skyline, and always has zero height. Also, the |
| 25 | +* ground in between any two adjacent buildings should be considered part of |
| 26 | +* the skyline contour. |
| 27 | +* |
| 28 | +* For instance, the skyline in Figure B should be represented as: |
| 29 | +* [ [2 10], [3 15], [7 12], [12 0], [15 10], [20 8], [24, 0] ]. |
| 30 | +* |
| 31 | +* Notes: |
| 32 | +* |
| 33 | +* The number of buildings in any input list is guaranteed to be in the range [0, 10000]. |
| 34 | +* The input list is already sorted in ascending order by the left x position Li. |
| 35 | +* The output list must be sorted by the x position. |
| 36 | +* There must be no consecutive horizontal lines of equal height in the output |
| 37 | +* skyline. For instance, [...[2 3], [4 5], [7 5], [11 5], [12 7]...] is not |
| 38 | +* acceptable; the three lines of height 5 should be merged into one in the |
| 39 | +* final output as such: [...[2 3], [4 5], [12 7], ...] |
| 40 | +*/ |
| 41 | + |
| 42 | +public class TheSkylineProblem218 { |
| 43 | +private static int L = 0; |
| 44 | +private static int R = 1; |
| 45 | +public List<int[]> getSkyline(int[][] buildings) { |
| 46 | +List<int[]> res = new ArrayList<>(); |
| 47 | +int N = buildings.length; |
| 48 | +if (N == 0) return res; |
| 49 | + |
| 50 | +// xi, hi, L/R |
| 51 | +int[][] verts = new int[N * 2][4]; |
| 52 | +int t = 0; |
| 53 | +int f = 0; |
| 54 | +for (int[] b: buildings) { |
| 55 | +verts[t++] = new int[]{b[0], b[2], L, f}; |
| 56 | +verts[t++] = new int[]{b[1], b[2], R, f}; |
| 57 | +f++; |
| 58 | +} |
| 59 | +Comparator<int[]> comp1 = new Comparator<int[]>() { |
| 60 | +@Override |
| 61 | +public int compare(int[] v1, int[] v2) { |
| 62 | +int xd = Integer.compare(v1[0], v2[0]); |
| 63 | +if (xd != 0) return xd; |
| 64 | + |
| 65 | +if (v1[2] == L && v2[2] == L) { |
| 66 | +return Integer.compare(v2[1], v1[1]); |
| 67 | +} else if (v1[2] == R && v2[2] == R) { |
| 68 | +return Integer.compare(v1[1], v2[1]); |
| 69 | +} else { |
| 70 | +return Integer.compare(v1[2], v2[2]); |
| 71 | +} |
| 72 | +} |
| 73 | +}; |
| 74 | +Arrays.sort(verts, comp1); |
| 75 | + |
| 76 | +Comparator<int[]> comp2 = (v1, v2) -> Integer.compare(v2[1], v1[1]); |
| 77 | +List<int[]> hs = new ArrayList<>(); |
| 78 | +for (int[] vi: verts) { |
| 79 | +int xi = vi[0]; |
| 80 | +int hi = vi[1]; |
| 81 | +int Di = vi[2]; |
| 82 | +int flag = vi[3]; |
| 83 | + |
| 84 | +Collections.sort(hs, comp2); |
| 85 | +if (Di == L) { // L |
| 86 | +if (hs.isEmpty() || hs.get(0)[1] < hi) { |
| 87 | +res.add(new int[]{xi, hi}); |
| 88 | +} |
| 89 | +hs.add(vi); |
| 90 | +} else { // R |
| 91 | +int size = hs.size(); |
| 92 | +for (int i=0; i<size; i++) { |
| 93 | +if (hs.get(i)[3] == flag) { |
| 94 | +hs.remove(i); |
| 95 | +break; |
| 96 | +} |
| 97 | +} |
| 98 | +if (hs.isEmpty() || hs.get(0)[1] < hi) { |
| 99 | +int y = hs.isEmpty() ? 0 : hs.get(0)[1]; |
| 100 | +res.add(new int[]{xi, y}); |
| 101 | +} |
| 102 | +} |
| 103 | +} |
| 104 | +return res; |
| 105 | +} |
| 106 | + |
| 107 | + |
| 108 | + |
| 109 | + |
| 110 | + |
| 111 | + |
| 112 | + |
| 113 | + |
| 114 | + |
| 115 | + |
| 116 | +} |
0 commit comments