Monday, October 19, 2020

The Skyline Problem

Problem: (Taken from leetcode) 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:

  1. The number of buildings in any input list is guaranteed to be in the range [0, 10000].
  2. The input list is already sorted in ascending order by the left x position Li.
  3. The output list must be sorted by the x position.
  4. 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], ...]


Approach: Using Merge sort approach (Divide and conquer); divide the given buildings in two subsets. Recursively construct skyline for two halves and finally merge the two skylines. Lets go to part of merging but before going there, let's understand what will happen if there is only one building with say {x, y, h} dimensions. It is easy to understand that the output skyline will be [{x, h}, {y, 0}]. 
Now let's see how to merge:
  • Say we have 2 subsets left_skyLines and right_skyLines. 
  • leftIndex = 0, rightIndex = 0, leftHeight = 0, rightHeight = 0
  • While leftIndex < left_skyLines.Size AND rightIndex < right_skyLines.Size
    • leftX = left_skyLines[leftIndex][0], rightX = right_skyLines[rightIndex][0]
    • curr_x = Min (leftX, rightY)
    • If leftX < rightX
      • leftHeight = left_skyLines[leftIndex][1]
      • leftIndex = leftIndex + 1
    • ELSE IF leftX > rightX
      • rightHeight = right_skyLines[rightIndex][1]
      • rightIndex = rightIndex + 1
    • ELSE
      • leftHeight = left_skyLines[leftIndex][1]
      • rightHeight = right_skyLines[rightIndex][1]
      • leftIndex = leftIndex + 1
      • rightIndex = rightIndex + 1
    • curr_height = Max(leftHeight, rightHeight)
    • Add {curr_x, curr_height} to result only of height of last element in result is not equal to curr_height to satisfy Note #4 in the problem statement.
  • Add the remaining skylines to result.
That's all! If you see it's just a merge sort and we just have to set right comparators.

Implementation in C#:

        public static IList<IList<int>> GetSkyline(int[][] buildings)

        {

            return GetSkylineRecur(buildings, 0, buildings.Length - 1);

        }


        private static List<IList<int>> GetSkylineRecur(int[][] buildings, int low, int high)

        {

            if (low > high)

            {

                return new List<IList<int>>();

            }

            if (low == high)

            {

                List<IList<int>> list = new List<IList<int>>();

                list.Add(new List<int> { buildings[low][0], buildings[low][2] });

                list.Add(new List<int> { buildings[low][1], 0 });

                return list;

            }

            int mid = (low + high) / 2;

            List<IList<int>> leftSkylines = GetSkylineRecur(buildings, low, mid);

            List<IList<int>> rightSkylines = GetSkylineRecur(buildings, mid + 1, high);

            return MergeSkylines(leftSkylines, rightSkylines);

        }


        private static List<IList<int>> MergeSkylines(List<IList<int>> leftSkylines, List<IList<int>> rightSkylines)

        {

            List<IList<int>> result = new List<IList<int>>();

            int hLeft = 0;

            int hRight = 0;

            int left = 0;

            int right = 0;

            while (left < leftSkylines.Count && right < rightSkylines.Count)

            {

                int currX;

                int currH;

                if (leftSkylines[left][0] < rightSkylines[right][0]) // comparing x 

                {

                    hLeft = leftSkylines[left][1];

                    currX = leftSkylines[left][0];

                    currH = Math.Max(hLeft, hRight);

                    ++left;

                }

                else if (leftSkylines[left][0] > rightSkylines[right][0])

                {

                    hRight = rightSkylines[right][1];

                    currX = rightSkylines[right][0];

                    currH = Math.Max(hLeft, hRight);

                    ++right;

                }

                else

                {

                    hLeft = leftSkylines[left][1];

                    hRight = rightSkylines[right][1];

                    currX = leftSkylines[left][0];

                    currH = Math.Max(hLeft, hRight);

                    ++left;

                    ++right;

                }

                if (result.Count <= 0 || result.Last()[1] != currH)

                {

                    result.Add(new List<int> { currX, currH });

                }

            }

            while(left < leftSkylines.Count)

            {

                result.Add(leftSkylines[left]);

                ++left;

            }

            while (right < rightSkylines.Count)

            {

                result.Add(rightSkylines[right]);

                ++right;

            }

            return result;

        }


Complexity: O(nLogn)

No comments:

Post a Comment