原题网址:https://leetcode.com/problems/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], ...]
方法一:使用最小堆保存建筑右边的x轴顺序,使用有序映射保存当前视野中的建筑高度。
public class Solution {
private List<int[]> skylines = new ArrayList<>();
private void append(int[] point) {
skylines.add(point);
if (skylines.size() == 1) return;
while (skylines.size()>=2) {
int[] prev = skylines.get(skylines.size()-2);
int[] last = skylines.get(skylines.size()-1);
if (prev[1] == last[1]) {
skylines.remove(skylines.size()-1);
} else if (prev[0] == last[0]) {
prev[1] = Math.max(prev[1], last[1]);
skylines.remove(skylines.size()-1);
} else {
break;
}
}
}
public List<int[]> getSkyline(int[][] buildings) {
TreeMap<Integer, Integer> heights = new TreeMap<>();
PriorityQueue<int[]> rights = new PriorityQueue<>(new Comparator<int[]>() {
@Override
public int compare(int[] nums1, int[] nums2) {
return Integer.compare(nums1[1], nums2[1]);
}
});
int i=0;
while (i<buildings.length || !rights.isEmpty()) {
while (!rights.isEmpty() && (i==buildings.length || rights.peek()[1] <= buildings[i][0])) {
int[] last = null;
while (!rights.isEmpty() && (last == null || rights.peek()[1] == last[1])) {
last = rights.poll();
int count = heights.remove(last[2]);
if (count>1) heights.put(last[2], count-1);
}
if (rights.isEmpty()) {
append(new int[] {last[1], 0});
} else {
int height = heights.lastKey();
if (last[2] > height) append(new int[] {last[1], height});
}
}
if (i<buildings.length) {
if (heights.isEmpty() || buildings[i][2] > heights.lastKey()) append(new int[] {buildings[i][0], buildings[i][2]});
rights.offer(buildings[i]);
Integer count = heights.get(buildings[i][2]);
if (count == null) count = 1; else count ++;
heights.put(buildings[i][2], count);
i ++;
}
}
return skylines;
}
}
另一种实现:
public class Solution {
private void add(int x, int h, List<int[]> skylines) {
if (!skylines.isEmpty() && skylines.get(skylines.size()-1)[1] == h) return;
if (!skylines.isEmpty() && skylines.get(skylines.size()-1)[0] == x) {
int[] skyline = skylines.get(skylines.size()-1);
skyline[1] = Math.max(skyline[1], h);
while (skylines.size() >= 2 && skylines.get(skylines.size()-2)[1] == skylines.get(skylines.size()-1)[1]) {
skylines.remove(skylines.size()-1);
}
return;
}
int[] skyline = new int[2];
skyline[0] = x;
skyline[1] = h;
skylines.add(skyline);
}
public List<int[]> getSkyline(int[][] buildings) {
List<int[]> skylines = new ArrayList<>();
Arrays.sort(buildings, new Comparator<int[]>() {
@Override
public int compare(int[] b1, int[] b2) {
return Integer.compare(b1[0], b2[0]);
}
});
PriorityQueue<Integer> rights = new PriorityQueue<>(new Comparator<Integer>() {
@Override
public int compare(Integer i1, Integer i2) {
return Integer.compare(buildings[i1][1], buildings[i2][1]);
}
});
PriorityQueue<Integer> heights = new PriorityQueue<Integer>(Collections.reverseOrder());
for(int i=0; i<buildings.length; i++) {
int[] building = buildings[i];
while (!rights.isEmpty() && buildings[rights.peek()][1] <= building[0]) {
int r = rights.poll();
heights.remove(buildings[r][2]);
if (buildings[r][1] == building[0]) continue;
if (!rights.isEmpty() && buildings[r][1] == buildings[rights.peek()][1]) continue;
int h = heights.isEmpty() ? 0 : heights.peek();
add(buildings[r][1], h, skylines);
}
rights.offer(i);
heights.offer(building[2]);
int h = heights.peek();
add(building[0], h, skylines);
}
while (!rights.isEmpty()) {
int r = rights.poll();
heights.remove(buildings[r][2]);
if (!rights.isEmpty() && buildings[r][1] == buildings[rights.peek()][1]) continue;
int h = heights.isEmpty() ? 0 : heights.peek();
add(buildings[r][1], h, skylines);
}
return skylines;
}
}
另一种实现:
public class Solution {
public List<int[]> getSkyline(int[][] buildings) {
List<int[]> results = new ArrayList<>();
PriorityQueue<Integer> rightHeap = new PriorityQueue<>(new Comparator<Integer>() {
@Override
public int compare(Integer i1, Integer i2) {
return Integer.compare(buildings[i1][1], buildings[i2][1]);
}
});
PriorityQueue<Integer> heightHeap = new PriorityQueue<>(new Comparator<Integer>() {
@Override
public int compare(Integer i1, Integer i2) {
return Integer.compare(buildings[i2][2], buildings[i1][2]);
}
});
int prev = 0;
for(int i = 0; i < buildings.length; i++) {
while (!rightHeap.isEmpty() && buildings[rightHeap.peek()][1] < buildings[i][0]) {
int b = rightHeap.poll();
heightHeap.remove(b);
while (!rightHeap.isEmpty() && buildings[rightHeap.peek()][1] == buildings[b][1]) {
b = rightHeap.poll();
heightHeap.remove(b);
}
int height = heightHeap.isEmpty() ? 0 : buildings[heightHeap.peek()][2];
if (height != prev) {
int[] result = new int[2];
result[0] = buildings[b][1];
result[1] = height;
results.add(result);
prev = height;
}
}
while (!rightHeap.isEmpty() && buildings[rightHeap.peek()][1] == buildings[i][0]) {
int b = rightHeap.poll();
heightHeap.remove(b);
}
rightHeap.offer(i);
heightHeap.offer(i);
while (i < buildings.length - 1 && buildings[i][0] == buildings[i + 1][0]) {
i++;
rightHeap.offer(i);
heightHeap.offer(i);
}
int height = buildings[heightHeap.peek()][2];
if (height != prev) {
int[] result = new int[2];
result[0] = buildings[i][0];
result[1] = height;
results.add(result);
prev = height;
}
}
while (!rightHeap.isEmpty()) {
int b = rightHeap.poll();
heightHeap.remove(b);
while (!rightHeap.isEmpty() && buildings[rightHeap.peek()][1] == buildings[b][1]) {
b = rightHeap.poll();
heightHeap.remove(b);
}
int height = heightHeap.isEmpty() ? 0 : buildings[heightHeap.peek()][2];
if (height != prev) {
int[] result = new int[2];
result[0] = buildings[b][1];
result[1] = height;
results.add(result);
prev = height;
}
}
return results;
}
}
方法二:对建筑的左边和右边合并在一起排序;保存当前视野内的建筑高度和前一个事业内的建筑高度,进行比较。
public class Solution {
public List<int[]> getSkyline(int[][] buildings) {
int[][] lines = new int[buildings.length*2][];
for(int i=0, j=0; i<buildings.length; i++) {
lines[j++] = new int[] {buildings[i][0], -buildings[i][2]};
lines[j++] = new int[] {buildings[i][1], buildings[i][2]};
}
Arrays.sort(lines, new Comparator<int[]>() {
@Override
public int compare(int[] n1, int[] n2) {
if (n1[0] != n2[0]) return n1[0] - n2[0];
return n1[1] - n2[1];
}
});
TreeMap<Integer, Integer> heights = new TreeMap<>();
heights.put(0, 1);
List<int[]> skylines = new ArrayList<>();
int prev = 0;
for(int[] line: lines) {
if (line[1] < 0) {
Integer count = heights.get(-line[1]);
if (count == null) count = 1; else count ++;
heights.put(-line[1], count);
} else {
Integer count = heights.remove(line[1]);
if (count > 1) heights.put(line[1], count-1);
}
int curr = heights.lastKey();
if (prev != curr) {
skylines.add(new int[] { line[0], curr });
prev = curr;
}
}
return skylines;
}
}
另一种实现:
public class Solution {
public List<int[]> getSkyline(int[][] buildings) {
List<int[]> skylines = new ArrayList<>();
int[][] lines = new int[buildings.length*2][];
for(int i=0, j=0; i<buildings.length; i++) {
lines[j++] = new int[] {buildings[i][0], -buildings[i][2]};
lines[j++] = new int[] {buildings[i][1], buildings[i][2]};
}
Arrays.sort(lines, new Comparator<int[]>() {
@Override
public int compare(int[] l1, int[] l2) {
if (l1[0] != l2[0]) return l1[0] - l2[0];
return l1[1] - l2[1];
}
});
PriorityQueue<Integer> heights = new PriorityQueue<>(Collections.reverseOrder());
int prev = 0;
for(int[] line: lines) {
if (line[1] < 0) {
heights.offer(-line[1]);
} else {
heights.remove(line[1]);
}
int curr = heights.isEmpty() ? 0 : heights.peek();
if (curr != prev) {
skylines.add(new int[]{line[0], curr});
prev = curr;
}
}
return skylines;
}
}