In the skyline problem, the user is provided with the coordinates of rectangular buildings that have varying widths and heights. The user is required to return a silhouette that traces the outlines of all the buildings.
Note: A city’s skyline is made up from the outer contour of the city’s silhouette. This silhouette can be seen when a city is viewed from a distance.
The simplest solution to this problem would be to use the sweeping line algorithm. However, before continuing, there are a few underlying assumptions that we need to state.
The basic idea of the sweeping line algorithm is to move a vertical line from the left end towards the right end. At any point, the height of the silhouette will be the maximum height of the building that the sweeping line is intersecting.
1. The c++
code will look like this:
for (int point = 0; point < numCriticalPoints; ++points){ for (int current = 0; current < numBuildings; ++current){ if (((buildings[current].start)< CriticalPoint[point]) && ((buildings[current].end)> CriticalPoint[point])){ set_of_buildings.insert(buildings[current]) } }
2. The c++
code would look like this:
if (set_of_buildings.size() == 0) { height_of_silhouette = 0 } else { height_of_silhouette = findMax(set_of_buildings) }
RELATED TAGS
View all Courses