Similar Problems

Similar Problems not available

The Skyline Problem - Leetcode Solution

Companies:

LeetCode:  The Skyline Problem Leetcode Solution

Difficulty: Hard

Topics: heap-priority-queue array  

The Skyline Problem is a classic problem in computer science that deals with finding the outline of a 2D city skyline. Given a list of buildings represented as a 3-tuple (left edge, right edge, and height), the goal is to determine the height profile of the city skyline, represented as a list of (x-coordinate, height) points.

The input to the problem is a list of buildings, where each building is represented as a tuple (left_edge, right_edge, height). The output should be a list of coordinates that represent the skyline. Each coordinate is represented as a tuple (x_coordinate, height).

Approach: We can solve this problem using the divide and conquer strategy. We will divide the input list of buildings into two halves and recursively find the skyline of each half. Then we will merge the two skylines to get the final result.

First find the middle index of the list and then divide it into two halves. This can be done using a simple binary search.

base case: If the list has only one building, return the skyline of that building, which is represented as two tuples (left_edge, height) and (right_edge, 0).

For the recursive case, we will merge the skylines of the left and right halves. The merge will be done by iterating over the two lists and keeping track of the last heights. The current heights will be determined by comparing the left height and right height. We will add the coordinate of the new point only if the height changes.

The algorithm will be implemented with the following steps:

  1. If there is only one building in the list, return the coordinates of the left and right edges of the building with a height value.

  2. Divide the list of buildings in two halves and recursively find the skylines of both halves.

  3. Merge the two skylines to get the final result. This can be done by iterating over both skylines and updating the current heights for each building encountered. If the height changes, add the new point to the final result.

  4. Return the final result.

Let's consider a simple example to illustrate the algorithm:

Input: [(2,9,10), (3,6,6), (5,12,12), (15,20,8), (17,25,6)]

Output: [(2,10), (3,6), (6,12), (12,0), (15,8), (20,0), (25,6)]

We start by dividing the input list into two halves:

[(2,9,10), (3,6,6), (5,12,12)] and [(15,20,8), (17,25,6)]

We recursively compute the skylines of both halves:

Left: [(2, 10), (9, 0), (3, 6), (6, 12), (5, 12), (12, 0)] Right: [(15, 8), (20, 0), (17, 6), (25, 6)]

We now merge the two skylines and get the final result:

[(2, 10), (3, 6), (6, 12), (12, 0), (15, 8), (20, 0), (25, 6)]

This is the expected output.

Time Complexity:

The time complexity of this algorithm is O(n log n), where n is the number of buildings in the input list. This is because we divide the input list into halves at each recursive step and merge the two halves in linear time.

Space Complexity:

The space complexity of this algorithm is O(n log n), where n is the number of buildings in the input list. This is because we store the result in a list of size n log n due to the recursive calls.

The Skyline Problem Solution Code

1