[Leedcode 218] 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 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], ...]

Credits:

 Public classSolution { Publiclist<int[]> Getskyline (int[] buildings) {       /*this question has a very ingenious idea: discretization. What do you mean? Since the high and the left and right borders of each building are integers, it is advisable to put the line segment on the whole point. Since the last one is for a contour, the highest point of the ordinate (height) is recorded for each horizontal axis.        The last one is scanned from left to right and outputs when the height is changed. But [[0,2147483647,2147483647]] results will be tle*/Map<Integer,Integer> map=NewHashmap<integer,integer>(); List<int[]> res=Newarraylist<int[]>(); if(buildings==NULL|| buildings.length<=0)returnRes; //int h[]=new int[buildings[buildings.length-1][1]];         for(inti=0;i<buildings.length;i++){             for(intj=buildings[i][0];j<buildings[i][1];j++){                if(!map.isempty () &&Map.containskey (j)) {                    intH=Map.get (j); H=math.max (buildings[i][2],h);                Map.put (J,H); }Else{map.put (j,buildings[i][2]); }                            }                    }         for(intI=1;i<map.size (); i++){            if(Map.get (i)!=map.get (i-1) {Res.add (New int[]{i,map.get (i)}); }        }        returnRes; }}

[Leedcode 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.