Introduction
Most people find The Skyline Problem a very complicated and hard problem to solve, although it is not so complicated. In this article, let me make this problem simple for you.
Before we start, if you don’t know about the Skyline Problem or if you want to give it a try yourself first, you can access the problem on Coding Ninjas Studio.
Recommended: Please try it on “Coding Ninjas Studio” before moving on to the solution approach.
So, let’s dive right into the actual problem statement.
Problem Statement
In a two-dimensional city, you are given 'N' rectangle buildings. Your job is to compute the skyline of these buildings, remove hidden lines, and return the skyline generated by all of them. When viewed from a distance, a city's skyline is the outside contour of the silhouette made by all of the buildings in that city. BUILDINGS[i] = [LEFT i, RIGHT i, HEIGHT i]: The geometric information of each building is given in the array of buildings:
-> LEFT_i is the x coordinate of the left edge of the ith building.
-> RIGHT_i is the x coordinate of the right edge of the ith building.
-> HEIGHT_i is the height of the ith building.
You may assume that all structures are perfect rectangles resting on a perfectly flat surface at a height of zero.
The skyline should be represented as a list of "key points" arranged by x-coordinate [[x1, y1], [x2, y2],...]. Except for the last point in the list, which always has a y-coordinate of 0 and is used to denote the skyline's conclusion where the rightmost building finishes, each key point is the left endpoint of some horizontal segment of the skyline. Any land between the leftmost and rightmost buildings should be included in the contour of the skyline.
NOTE:
In the output skyline, there must be no consecutive horizontal lines of similar height. For example, [...,[2 3], [4 5], [7 5], [11 5], [12 7],...] is not acceptable; in the final output, the three lines of height 5 should be merged into one.
For Example [..., [2 3], [4 5], [12 7],...]
In addition, the structures are arranged in a non-descending order.
Input Format
The first input line contains a single integer 'N,’ which represents the number of buildings to be provided.
The remaining 'N' lines include three space-separated integers: the left and right indices of the building's vertical edges, and the last integer specifies the building's height 'H.’
Output Format
Return a list of all the skylines that these buildings have created.
[[x1, y1], [x2, y2],...] should be used to represent the skyline as a list of "key points" arranged by their x-coordinate. Each key point is located on the left side of the page.
Except for the last point in the list, which always has a y-coordinate of 0 and is used to mark the termination of some horizontal segment in the skyline.
The rightmost building is when the skyline comes to an end. Any land between the leftmost and rightmost buildings should be included in the contour of the skyline.
For more clarification, see the below example.
Example
Assume we have the following input:
INPUT:
5
2 9 10
3 7 15
5 12 12
12 20 10
19 24 8
OUTPUT:
2 10
3 15
7 12
12 0
15 10
20 8
24 0
Explanation:
The below figure shows the buildings of the input. The adjacent figure shows the skyline formed by those buildings.
The red points in the adjacent figure represent the height of points in the output list.
Also read, Euclid GCD Algorithm