LeetCode Q218 The Skyline Problem

Question:

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).

Buildings Skyline Contour

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:

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

Solution: O(n * logn) –804ms Space O(n);
Maximum Heap

class mycomparison{
public:
  bool operator() ( pair&lt;int, int&gt; &lhs, pair&lt;int, int&gt; &rhs ){
    if (lhs.second == rhs.second )
        return lhs.first &lt; rhs.first;
    return (lhs.second &lt; rhs.second);
  }
};

class Solution {
public:
    vector&lt;pair&lt;int, int&gt;&gt; getSkyline(vector&lt;vector&lt;int&gt;&gt;& buildings) {
        vector&lt;pair&lt;int, int&gt;&gt; res;
        int n = buildings.size(), i = 0;
        priority_queue&lt;pair&lt;int, int&gt;,vector&lt;pair&lt;int, int&gt;&gt;, mycomparison&gt; queue;
            
        while( i &lt; n || !queue.empty() ){
            if ( queue.empty() ){ // if queue is empty push start to res, and push end to queue.
             res.push_back( make_pair(buildings[i][0], buildings[i][2] ) );
             queue.push( make_pair(buildings[i][1], buildings[i][2] ) );
                i++;
            }
            else if ( i == n || buildings[i][0] &gt; queue.top().first ){ // if new rectangle's start &gt; previous rectangle's end pop the heighest end
                pair&lt;int, int&gt; heighest = queue.top();
                queue.pop();
                if ( queue.empty() )
                    res.push_back( make_pair( heighest.first, 0 ) );
                else {
                    while( !queue.empty() && queue.top().first &lt;= heighest.first ) // **delete these lines that before the heighest end line
                        queue.pop();
                    res.push_back(make_pair( heighest.first, queue.empty() ? 0 : queue.top().second ));
                }
            }
            else { 
                if ( buildings[i][2] &gt; res.back().second ) // if new start line's height heigher than previous line push it to res.
                    res.push_back( make_pair( buildings[i][0], buildings[i][2] ) );
                if ( queue.empty() || buildings[i][2] &gt; queue.top().second || buildings[i][1] &gt; queue.top().first ) // if queue is empty or current rectangle's end line's height is the heightest one, or this end line after the heighest end line push it to the queue. for the last case we have to **delete those lines before it and shorter than it. 
                    queue.push( make_pair( buildings[i][1], buildings[i][2] ) );
                i++;
            }
        }
        return res;
    }
};

Solution2: divided and conquer similar to merge sort

class Solution {
public:
    void removeItems( vector&lt;pair&lt;int, int&gt;&gt; &res ){
        vector&lt;pair&lt;int, int&gt;&gt; copy{ res[0] };
        for(int i = 1; i &lt; res.size(); i++)
            if (res[i].second != copy.back().second)
                copy.push_back(res[i]);
        res = copy;
        
    }
    vector&lt;pair&lt;int, int&gt;&gt; merge( vector&lt;pair&lt;int, int&gt;&gt; &l1, vector&lt;pair&lt;int, int&gt;&gt; &l2){
        vector&lt;pair&lt;int, int&gt;&gt; res;
        int i = 0, j = 0, h1 = 0, h2 = 0;
        while( i &lt; l1.size() && j &lt; l2.size() ){
            if( l1[i].first &lt; l2[j].first ) {
                h1 = l1[i].second;
                res.push_back( make_pair( l1[i].first, max( h1, h2 ) ) );
                i++;
            }
            else if(l1[i].first &gt; l2[j].first){
                h2 = l2[j].second;
                res.push_back(make_pair(l2[j].first, max(h1, h2)));
                j++;
               
            }
            else {
                h1 = l1[i].second;
                h2 = l2[j].second;
                res.push_back( make_pair(l1[i].first, max( h1, h2 )));
                i++;
                j++;
            }
        }
        if ( i &lt; l1.size() )
            res.insert(res.end(), l1.begin() + i, l1.end() );
        if ( j &lt; l2.size() )
            res.insert(res.end(), l2.begin() + j, l2.end() ) ;
        //removeItems(res);
        return res;
    }
    
    
    vector&lt;pair&lt;int, int&gt;&gt; findSkyline(vector&lt;vector&lt;int&gt;&gt;& buildings, int start, int end){
        vector&lt;pair&lt;int, int&gt;&gt; res;
        if ( start == end ){ //base case
            return  vector&lt;pair&lt;int, int&gt;&gt;{ make_pair(buildings[start][0], buildings[start][2]), make_pair(buildings[start][1], 0) };
        }
        int mid = ( start + end ) / 2;
        vector&lt;pair&lt;int, int&gt;&gt; left = findSkyline( buildings, start, mid );
        vector&lt;pair&lt;int, int&gt;&gt; right = findSkyline( buildings, mid+1, end );
        return merge( left, right );
       
    }
    vector&lt;pair&lt;int, int&gt;&gt; getSkyline(vector&lt;vector&lt;int&gt;&gt;& buildings) {
        vector&lt;pair&lt;int, int&gt;&gt; res;
        if ( buildings.size() &gt; 0 ){
            res = findSkyline(buildings, 0, buildings.size() - 1);
            removeItems(res);
        }
        return res;
    } 
};

One thought on “LeetCode Q218 The Skyline Problem

Leave a comment