218. The Skyline problem

Source: Internet
Author: User
Tags addall

A city's skyline is the outer contour of the silhouette formed by all the buildings in then city then viewed from a Distan Ce. Now suppose you is given the locations and height of all the buildings as shown in 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 was represented by a triplet of integers [Li, Ri, Hi] , where and is the Li Ri x Coordinates of the left and right edge of the ith building, respectively, and are its Hi height. It's guaranteed that 0 ≤ Li, Ri ≤ INT_MAX , 0 < Hi ≤ INT_MAX and Ri - Li > 0 . Assume all buildings is perfect rectangles grounded on a absolutely flat surface at height 0.

For instance, the dimensions of all buildings in Figure A is 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 that [ [x1,y1], [x2, y2], [x3, y3], ... ] uniquely defines a skyline. A key point was the left endpoint of a horizontal line segment. Note the last key point, where the rightmost building ends, was merely used to mark the termination of the skyline, an D always have zero height. Also, the ground in between any and adjacent buildings should is considered part of the Skyline Contour.

For instance, the skyline in Figure B should is represented as: [ [2 10], [3 15], [7 12], [12 0], [15 10], [20 8], [24, 0] ] .

Notes:

    • The number of buildings in all input list is guaranteed to being in the range [0, 10000] .
    • The input list is already sorted in ascending order by the left X position Li .
    • The output list must is sorted by the X position.
    • There must is no consecutive horizontal lines of equal height in the output skyline. For instance, was not [...[2 3], [4 5], [7 5], [11 5], [12 7]...] acceptable; The three lines of height 5 should being merged into one in the final output as such:[...[2 3], [4 5], [12 7], ...]

Points:skyline mean set of those Points who change hight

1 /*2 * Solution 1 Heap--Priorityqueue3  */4  Public classSolution {5      Publiclist<int[]> Getskyline (int[] buildings) {6list<int[]> list =Newlinkedlist<int[]>();7list<int[]> height =Newlinkedlist<int[]>();8         9          for(int[] b:buildings) {TenHeight.add ( new int[]{b[0],-b[2]}); OneHeight.add (New int[]{b[1], b[2]}); A         } -          -         Collections.sort(Height, (A, B), {  the             if(a[0]! = b[0]) -                 returnA[0]-b[0];//Small to large -             returnA[1]-b[1];// Low-to-high -  }); +          -Priorityqueue<integer> PQ =NewPriorityqueue<integer> ((A, B)(b- a)); +          int pre = 0;  A         pq.offer (0);  at          -          for(int[] h:height) { -             if(H[1] < 0) { -Pq.offer (-h[1]); -}Else { -Pq.Remove(h[1]); in             } -             intCur =Pq.peek (); to             if(Pre! =cur) { //  change of Height point +List.add (New int[]{h[0], cur}); -Pre =cur; the             } *         } $         returnlist;Panax Notoginseng     } -}
1 /*2 * Solution 2 Divide and conquer--merge3  */4  Public classSolution {5      Publiclist<int[]> Getskyline (int[] buildings) {6         if(Buildings.length = = 0)7             return Newlinkedlist<int[]>();8         returnRecurskyline (buildings, 0, buildings.length-1);9     }Ten      One     Private LinkedList<int[]> Recurskyline (int[] Buildings,intSintt) { A         if(s = =t) { -linkedlist<int[]> ret =Newlinkedlist<int[]>(); -Ret.add (New int[]{buildings[s][0], buildings[s][2]});//Left top theRet.add (New int[]{buildings[s][1], 0});//Right , bottom. -             returnret; -}Else { -             intMID = (T-s)/2 +s; +             returnMerge (Recurskyline (buildings, S, mid), Recurskyline (buildings, mid + 1, T)); -         } +     } A      at     Private LinkedList<int[]> Merge (linkedlist<int[]> L1, linkedlist<int[]>L2) { -         intH = 0; -         intx = 0; -         intH1 = 0, h2 = 0; -linkedlist<int[]> ret =Newlinkedlist<int[]>(); -          in          while(!l1.isempty () &&!L2.isempty ()) { -             if(L1.getfirst () [0] < L2.getfirst () [0]) { tox = L1.GetFirst() [0]; //GetFirst, Removefirst, getlast based on LinkedList, List  does not have these functions +H1 = L1.getfirst () [1]; -h =Math.max (H1, H2); the L1.removefirst (); *}Else if(L1.getfirst () [0] > L2.getfirst () [0]) { $x = L2.getfirst () [0];Panax NotoginsengH2 = L2.getfirst () [1]; -h =Math.max (H1, H2); the L2. Removefirst (); +}Else { Ax = L1.getfirst () [0]; theH1 = L1.getfirst () [1]; +H2 = L2.getfirst () [1]; -h =Math.max (H1, H2); $ L1.removefirst (); $ L2.removefirst (); -             } -              the             if(ret.size () = = 0 | | ret.getlast () [1]! = h) {//Height Change point -Ret.add (New int[]{x, H}];Wuyi             } the         } -          Wu Ret.addall (L1); - Ret.addall (L2); About         returnret; $     } -}

218. The Skyline problem

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.