Leetcode:the Skyline problem

Source: Internet
Author: User

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 Li and Ri are T He x coordinates of the left and right edge of the ith building, respectively, and Hi are its height. It's Guaranteed that0≤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 [[X1,y1], [X2, Y2], [X3, Y3], ...] that 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 any input list is guaranteed to being in the range [0, 10000]. The input list is already sorted on ascending order by the left X position li.the output list must being sorted by the X posi tion. There must is 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 is merged into one in theFinalOutput as such: [... [2 3], [4 5], [12 7], ...]

Reference to the Http://www.cnblogs.com/tonyluis/p/4564617.html

Don't understand why 16 lines are b[1]-a[1]

1  Public classSolution {2      Publiclist<int[]> Getskyline (int[] buildings) {3list<int[]> res =Newarraylist<int[]>();4Priorityqueue<integer> maxheap =NewPriorityqueue<integer> (11,NewComparator<integer>(){5              Public intCompare (integer A, integer b) {6                 returnB-A;7             }8         });9list<int[]> edges =Newarraylist<int[]>();Ten          for(int[] building:buildings) { OneEdges.add (New int[]{building[0], building[2]}); AEdges.add (New int[]{building[1],-building[2]}); -         } -Collections.sort (Edges,Newcomparator<int[]>() { the              Public intCompareint[] A,int[] b) { -                 returnA[0]==b[0]? B[1]-A[1]: a[0]-b[0]; -             } -         }); +          -         intCur=0, pre=0; +          for(int[] edge:edges) { A             if(Edge[1] > 0) { atMaxheap.add (edge[1]); -Cur =Maxheap.peek (); -             } -             Else { -Maxheap.remove (-edge[1]); -cur = maxheap.isempty ()? 0: Maxheap.peek (); in             } -             if(cur! =pre) { toRes.add (New int[]{edge[0], cur}); +Pre =cur; -             } the         } *          $         returnRes;Panax Notoginseng     } -}

Leetcode: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.