05 August 2008
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
, where 1
[Li, Ri, Hi]
and 1
Li
are the x coordinates of the left and right edge of the ith building, respectively, and 1
Ri
is its height. It is guaranteed that 1
Hi
, 1
0 ≤ Li, Ri ≤ INT_MAX
, and 1
0 < Hi ≤ INT_MAX
. You may assume all buildings are perfect rectangles grounded on an absolutely flat surface at height 0.1
Ri - Li > 0
For instance, the dimensions of all buildings in Figure A are recorded as:
.1
[ [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
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.1
[ [x1,y1], [x2, y2], [x3, y3], ... ]
For instance, the skyline in Figure B should be represented as:
.1
[ [2 10], [3 15], [7 12], [12 0], [15 10], [20 8], [24, 0] ]
Notes:
1
[0, 10000]
.1
Li
.1
[...[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: 1
[...[2 3], [4 5], [12 7], ...]
这里有大牛的解释(https://briangordon.github.io/2014/08/the-skyline-problem.html),使用了Sweep Line扫描线算法,该算法在meeting room的题目也会遇到,首先从左到右扫描就会知道每个点的位置,其次判断需要知道的点起始点-即为最高点,终止点即为第二高点。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
class Solution {
public List<int[]> getSkyline(int[][] buildings) {
List<int[]> result = new ArrayList<>();
List<int[]> heights = new ArrayList<>();
for (int[] building : buildings) {
heights.add(new int[]{building[0], -building[2]});
heights.add(new int[]{building[1], building[2]});
}
Collections.sort(heights, (a, b) -> {
if (a[0] !=b[0]) {
return a[0] - b[0];
}
return a[1] - b[1];
});
Queue<Integer> pq = new PriorityQueue<>((a, b) -> (b - a));
pq.offer(0);
int prev = 0;
for (int[] height : heights) {
if (height[1] < 0) {
pq.offer(-height[1]);
} else {
pq.remove(height[1]);
}
int current = pq.peek();
if (prev != current) {
result.add(new int[]{height[0], current});
prev = current;
}
}
return result;
}
}