© Parineeth M R

Question 61. Find the skyline of a group of buildings

The skyline problem is pretty popular in coding interviews. Here we take an in depth look at how to solve the problem.

Consider the diagram below where there are 4 buildings: B1 (height = 20), B2 (height = 30), B3 (height = 40) and B4 (height = 10).

The resulting skyline of the buildings can be obtained by picking the height of the tallest building at each position. So for instance, at position = 1, the height of the tallest building is 20. At position = 2, the height of the tallest building is 40. The resulting skyline for the buildings is shown below

To solve the problem, let us first develop a notation for representing each building and a notation for representing the resulting skyline. Each building can be represented with its left position, right position and its height. So for instance in the diagram, building B1 can be represented as (left = 1, right = 4, height = 20).

Strip S1 is represented using point P1 (position = 1, height = 20).

Strip S2 is represented using point P2 (position = 2, height = 40).

Strip S3 is represented using point P3 (position = 6, height = 10).

When we indicate the point for the strip, it means that the strip starts at this point and the height of the strip continues till the next skyline point. So in the diagram above for strip S2 having point P2 (position = 2, height = 40), it means that the strip starts at position 2 and has a height of 40 till we reach the next point P3 at position 6

Note that the buildings given to us in the input need not be sorted based on the left position of the building. So for instance, the buildings can be input in the following order: B2, B4, B1, B3.

Brute force – In the brute force technique, we first start with a single building and compute the skyline of the first building. Then we go on adding one building at a time and re-compute the result skyline. This is similar to insertion sort. The result skyline will maintain the points in sorted order based on position. Each time we add a building, we have to find where we can insert it in the result skyline. This operation is O(n). Since we have to do this operation for n buildings, the time complexity is O(n2).

There is a more efficient solution that uses merge sort with time complexity O(nlogn). Given a group of unsorted buildings, we divide the group into two groups: G1 and G2. We then recursively find the skylines for the two groups and then merge the skylines of the two groups to get the result.

So in our example, as we recursively divide the group of buildings, we will finally reach the stage where we get individual buildings B1, B2, B3 and B4. Once we reach this stage, we can represent each building as a strip as shown in the diagram below

Each strip indicates the skyline for a single building. Now we need to recursively merge the skylines to get the final result. Each skyline formed during the merge can consist of one or more strips. The basic idea that we use to merge the skylines is to pick the maximum height of the two input skylines at all the positions. So we maintain two variables: H1 and H2. H1 is the current height of skyline of group 1 and H2 is the current height of skyline in group 2. H1 and H2 are initialized to 0

To merge the two skylines, traverse the two skylines, keep picking the left most point in the two skylines and perform the following

1. If the left most point lies in Group-1, then update the height H1 to the height of the left most point. The height of the result skyline at this point is H = max(H1, H2). Let the position of the left most point be P. Append a new point (P, H) to the result skyline. Then advance to the next point in Group-1.

2. If the left most point lies in Group-2, then update the height H2 to the height of the left most point. The height of the result skyline at this point is H = max(H1, H2). Let the position of the left most point be P. Append a new point (P, H) to the result skyline. Then advance to the next point in Group-2.

3. If the left most point in Group-1 is at the same position as the left most point in Group-2, then update H1 to the height of the left most point in Group-1 and H2 to the height of the left most point in Group-2. The height of the result skyline at this point is H = max(H1, H2). Let the position of the left most point be P. Append a new point (P, H) to the result skyline. Then advance to the next point in Group-1 and Group-2.

When appending a new point to the result skyline, we observe the following rules

– We will not maintain redundant strips in the result skyline. So if the height of new point being added to the result skyline is equal to the height of the last point in the result skyline, then there is no need to add the new point to the result skyline.

– There can’t be two points at the same position in the result skyline. So if the position of the new point being added to the result skyline is equal to the position of the last point in the result skyline, then pick maximum height of the new point and old point and store the maximum height at this position.

Consider the case where we are merging the skylines of building B1 and B2 to get the skyline R1.

At position P1, the height of building B1 is H1 = 20 and height of building B2 is H2 = 0. So max (H1, H2) = 20. So height of result skyline at P1 is 20.

At position P2, the height of B1 is H1 = 20 and height of B2 is H2 = 30. So max(H1, H2) = 30. So height of result skyline at P2 is 30.

At position P3, the height of B1 is H1 = 20 and height of B2 is H2 = 0. So max(H1, H2) = 20. So height of result skyline at P3 is 20.

At position P4, the height of B1 is H1 = 0 and height of B2 is H2 = 0. So max(H1, H2) = 0. So height of result skyline at P4 is 0.

The merging of the skylines B3 and B4 to get the skyline R2 is shown in the diagram below.

The merging of skyline R1 and R2 to get the final skyline result is shown below

The code for computing the skyline is given below