218. The Skyline Problem (Hard)
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], ...]
Solutions
class Solution {
class Building {
int x = 0;
int y = 0;
int height = 0;
public Building(int x, int y, int height) {
this.x = x;
this.y = y;
this.height = height;
}
}
public List<List<Integer>> getSkyline(int[][] buildings) {
List<List<Integer>> ans = new ArrayList<>();
if (buildings == null || buildings.length == 0) {
return ans;
}
// sort the buildings by x by ascending order.
PriorityQueue<Building> queue = new PriorityQueue<>(Comparator.comparingInt(o -> o.x));
for (int i = 0; i < buildings.length; i++) {
queue.add(new Building(buildings[i][0], buildings[i][1], buildings[i][2]));
}
List<Building> tmp = new ArrayList<>();
Building pre = queue.poll();
while (queue.size() > 0) {
// merge current building and previous one
Building cur = queue.poll();
// no overlaps, cur.x > pre.y
if (cur.x >= pre.y) {
tmp.add(pre);
pre = cur;
continue;
}
// with overlaps, cur.x < pre.y, split pre and cur into several pieces
// if cur.y == pre.y pre and cur will be represented by following two buildings,
// [pre.x, cur.x] and [cur.x, pre.y]
if (cur.y == pre.y) {
// the contour height of building [pre.x, cur.x]
int height1 = pre.height;
// if pre.x == cur.x, ignore it
if (pre.x != cur.x) {
queue.add(new Building(pre.x, cur.x, height1));
}
// the contour height of building [cur.x, pre.y]
int height2 = Math.max(pre.height, cur.height);
queue.add(new Building(cur.x, pre.y, height2));
pre = queue.poll();
continue;
}
// cur.y < pre.y, pre and cur will be represented by following three buildings,
// [pre.x, cur.x], [cur.x, cur.y] and [cur.y, pre.y]
if (cur.y < pre.y) {
// the contour height of building [pre.x, cur.x]
int height1 = pre.height;
// if pre.x == cur.x, ignore it
if (pre.x != cur.x) {
queue.add(new Building(pre.x, cur.x, height1));
}
// the contour height of building [cur.x, cur.y]
int height2 = Math.max(pre.height, cur.height);
queue.add(new Building(cur.x, cur.y, height2));
// the contour height of building [cur.y, pre.y]
int height3 = pre.height;
queue.add(new Building(cur.y, pre.y, height3));
pre = queue.poll();
continue;
}
// cur.y > pre.y, pre and cur will be represented by following three buildings,
// [pre.x, cur.x], [cur.x, pre.y] and [pre.y, cur.y]
if (cur.y > pre.y) {
// the contour height of building [pre.x, cur.x]
int height1 = pre.height;
// if pre.x == cur.x, ignore it
if (pre.x != cur.x) {
queue.add(new Building(pre.x, cur.x, height1));
}
// the contour height of building [cur.x, pre.y]
int height2 = Math.max(pre.height, cur.height);
queue.add(new Building(cur.x, pre.y, height2));
// the contour height of building [pre.y, cur.y]
int height3 = cur.height;
queue.add(new Building(pre.y, cur.y, height3));
pre = queue.poll();
continue;
}
}
tmp.add(new Building(pre.x, pre.y, pre.height));
// merge the consecutive buildings that of same height
pre = tmp.get(0);
for (int i = 1; i < tmp.size(); i++) {
Building cur = tmp.get(i);
// not consecutive
if (cur.x != pre.y) {
ans.add(Arrays.asList(pre.x, pre.height));
// set height 0 for space without buildings
ans.add(Arrays.asList(pre.y, 0));
pre = cur;
continue;
}
// two consecutive buildings
// merge if of same height
if (pre.height == cur.height) {
pre.y = cur.y;
// do not change the pre
}
// not the same height, we need to distinguish them
else {
ans.add(Arrays.asList(pre.x, pre.height));
pre = cur;
}
}
ans.add(Arrays.asList(pre.x, pre.height));
ans.add(Arrays.asList(pre.y, 0));
return ans;
}
}