|
1 | | -H |
| 1 | +R |
| 2 | +1521176156 |
| 3 | +tags: Divide and Conquer, Heap, Binary Indexed Tree, Segment Tree, Sweep Line |
2 | 4 |
|
3 | | -又叫做skyline |
| 5 | +又叫做skyline. 用Sweep Line做的O(nLogN), 但是貌似还有很多做法: segement tree, hashheap, treeSet? |
4 | 6 |
|
5 | | -看网上的解答做, 思路很漂亮。 (http://codechen.blogspot.com/2015/06/leetcode-skyline-problem.html?_sm_au_=isVmHvFmFs40TWRt) |
| 7 | +#### 方法1: Sweep Line, Time O(nLogN), Space O(n) |
| 8 | +original reference http://codechen.blogspot.com/2015/06/leetcode-skyline-problem.html?_sm_au_=isVmHvFmFs40TWRt |
6 | 9 |
|
7 | | -跟scan line的approach类似: |
8 | | -1. 把所有点分出来, 每个点有index x, 再加上一个height. |
9 | | -2. 在这个list上排序,根据index和height(注意用负数标记building start point,这样保证start在end 之前。). 叫做 heightPoints |
10 | | -3. 在processs时候用max-heap (reversed priorityqueue),在ieteraete heightPoints 来存最大的height . 遇到peek,就是一个合理的解 |
11 | | - 处理1:因为start,end的height都存在了heightPoints里面,这里就是用来check end of bulding的,然后把height 从queue里面remove. |
12 | | - 处理2:重复x 上面的许多height? priorityqueue给了我们最高,这okay了;那么其他的重复点,用一个int prev来mark之前做过的,一旦重复,跳过。 |
| 10 | +sweep line: |
| 11 | +- 把所有点分出来, 每个点有index x, 再加上一个height. |
| 12 | +- 在这个list上排序,根据index和height. 注意用负数标记building start point height, 这样保证start在end 之前 |
| 13 | +- 用负数的height标记start: 在priority queue里面同一个x-pos比较 startPoint.height, endPoint.height 的时候, 因为end height是整数, 所以compare时会自动把start point放在end point前面 |
| 14 | +- 当然了, 如果两个 start point比较, 第二个point的负数超大的话(也就是height很高), 就会顺理compare return正数, 成章形成倒位 |
| 15 | +- 在processs时候用max-heap (reversed priorityqueue),再iterate heightPoints 来存最大的height . 遇到peek,就是一个合理的解 |
| 16 | +- heightQueue里面加一个0, 用来在结尾的时候做closure |
13 | 17 |
|
14 | | -想法非常natural。 大题目,脑子乱。 |
15 | | -看了解答再去想,挺naturally doable的。 |
| 18 | +#### 方法2: Segment Tree |
| 19 | +REVIEW |
| 20 | + |
| 21 | +Binary Indexed Tree? |
| 22 | + |
| 23 | +HashHeap? |
16 | 24 |
|
17 | 25 | ``` |
| 26 | +/* |
| 27 | +LeetCode: |
| 28 | +A city's skyline is the outer contour of the silhouette formed by all the buildings in that city |
| 29 | +when viewed from a distance. Now suppose you are given the locations and height of all the buildings |
| 30 | +as shown on a cityscape photo (Figure A), write a program to output the skyline formed by these buildings |
| 31 | +collectively (Figure B). |
| 32 | +
|
| 33 | +The geometric information of each building is represented by a triplet of integers [Li, Ri, Hi], |
| 34 | +where Li and Ri are the x coordinates of the left and right edge of the ith building, respectively, |
| 35 | +and Hi is its height. It is guaranteed that 0 ≤ Li, Ri ≤ INT_MAX, 0 < Hi ≤ INT_MAX, and Ri - Li > 0. |
| 36 | +You may assume all buildings are perfect rectangles grounded on an absolutely flat surface at height 0. |
| 37 | +
|
| 38 | +For instance, the dimensions of all buildings in Figure A are recorded as: |
| 39 | +[ [2 9 10], [3 7 15], [5 12 12], [15 20 10], [19 24 8] ] . |
| 40 | +
|
| 41 | +The output is a list of "key points" (red dots in Figure B) in the format of |
| 42 | +[ [x1,y1], [x2, y2], [x3, y3], ... ] that uniquely defines a skyline. |
| 43 | +A key point is the left endpoint of a horizontal line segment. Note that the last key point, |
| 44 | +where the rightmost building ends, is merely used to mark the termination of the skyline, |
| 45 | +and always has zero height. Also, the ground in between any two adjacent buildings |
| 46 | +should be considered part of the skyline contour. |
| 47 | +
|
| 48 | +For instance, the skyline in Figure B should be represented as: |
| 49 | +[ [2 10], [3 15], [7 12], [12 0], [15 10], [20 8], [24, 0] ]. |
| 50 | +
|
| 51 | +Notes: |
| 52 | +
|
| 53 | +The number of buildings in any input list is guaranteed to be in the range [0, 10000]. |
| 54 | +The input list is already sorted in ascending order by the left x position Li. |
| 55 | +The output list must be sorted by the x position. |
| 56 | +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], ...] |
| 57 | +
|
| 58 | +*/ |
| 59 | + |
| 60 | +/* |
| 61 | +Thoughts: |
| 62 | +Sweep Line. |
| 63 | +1. Store the building start/end points into a Point object. |
| 64 | +2. PriorityQueue on the Point: height low -> high && position lowX -> highX |
| 65 | +3. Loop over PointQueue, and store the heights in a separate maxQueue |
| 66 | +4. Based on the currPeak and prevPeak to detect diff: when there is a diff, there is a recording point. |
| 67 | + Keeping add start height into the maxHeap, and remove end points whenever facing one. |
| 68 | +
|
| 69 | +*/ |
| 70 | +class Solution { |
| 71 | + class Point { |
| 72 | + int pos; |
| 73 | + int height; |
| 74 | + public Point(int pos, int height) { |
| 75 | + this.pos = pos; |
| 76 | + this.height = height; |
| 77 | + } |
| 78 | + } |
| 79 | + public List<int[]> getSkyline(int[][] buildings) { |
| 80 | + List<int[]> rst = new ArrayList<>(); |
| 81 | + if (buildings == null || buildings.length == 0 || buildings[0] == null || buildings[0].length == 0) { |
| 82 | + return rst; |
| 83 | + } |
| 84 | + int m = buildings.length; |
| 85 | + // Sort points(xPos, height, isStart:boolean) by height && xPos with queue |
| 86 | + PriorityQueue<Point> queue = new PriorityQueue<Point>(new Comparator<Point>() { |
| 87 | + public int compare(Point a, Point b) { |
| 88 | + if (a.pos == b.pos) { |
| 89 | + return a.height - b.height; |
| 90 | + } else { |
| 91 | + return a.pos - b.pos; |
| 92 | + } |
| 93 | + } |
| 94 | + }); |
| 95 | + for (int i = 0; i < m; i++) { |
| 96 | + queue.offer(new Point(buildings[i][0], -buildings[i][2])); |
| 97 | + queue.offer(new Point(buildings[i][1], buildings[i][2])); |
| 98 | + } |
| 99 | + |
| 100 | + // Mark height and calcualte the outline point. |
| 101 | + PriorityQueue<Integer> maxHeightQueue = new PriorityQueue<>(Collections.reverseOrder()); |
| 102 | + maxHeightQueue.offer(0); |
| 103 | + int prevPeak = maxHeightQueue.peek(); |
| 104 | + |
| 105 | + while (!queue.isEmpty()) { |
| 106 | + Point point = queue.poll(); |
| 107 | + if (point.height < 0) { |
| 108 | + maxHeightQueue.offer(-point.height); |
| 109 | + } else { // is end point of a building |
| 110 | + maxHeightQueue.remove(point.height); |
| 111 | + } |
| 112 | + |
| 113 | + int currPeak = maxHeightQueue.peek(); |
| 114 | + if (currPeak != prevPeak) { |
| 115 | + rst.add(new int[]{point.pos, currPeak}); |
| 116 | + prevPeak = currPeak; |
| 117 | + } |
| 118 | + } |
| 119 | + return rst; |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | + |
18 | 124 | /* |
19 | 125 | LintCode description: |
20 | 126 | Given N buildings in a x-axis,each building is a rectangle and can be represented by a triple (start, end, height), |
|
0 commit comments