Leetcode 218:the Skyline problem

Source: Internet
Author: User

The Skyline problemTotal Accepted: 1071 Total Submissions: 6525

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 isgiven the locations and height of all the buildings as shown in a cityscape photo (figure a), Write a program tooutput the skyline formed by these buildings collectively (Figure B).

Ref=2a0e4qu ">

The geometric information of each building was represented by a triplet of integers [Li, Ri, Hi] , where and is the Li Ri x Co Ordinates 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 keypoint 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], ...]

Ideas

Get up and write again:)

[CODE]

public class Solution {public list<int[]> getskyline (int[][] buildings) {list<int[]> res = new ARR        Aylist<int[]> (); priorityqueue<integer> maxheap = new Priorityqueue<integer> (one, new comparator<integer> () {@O            Verride public int Compare (integer A, integer b) {return b-a;        }        });        list<int[]> bl = new arraylist<int[]> ();                for (int i=0; i<buildings.length; i++) {int[] b = buildings[i];            Bl.add (New int[]{b[0], b[2]});        Bl.add (New int[]{b[1],-b[2]});  } collections.sort (BL, new comparator<int[]> () {@Overridepublic int compare (int[] A, int[] b)        {if (a[0]!=b[0]) return a[0]-b[0];        else return b[1]-a[1];        }        });        int pre = 0, cur = 0;            for (int i=0; i<bl.size (); i++) {int[] b = bl.get (i); if (b[1]>0) {MaxheAp.add (b[1]);            cur = Maxheap.peek ();                } else {Maxheap.remove (-b[1]); Cur = (Maxheap.peek () ==null)?            0:maxheap.peek ();                } if (Cur!=pre) {res.add (new int[]{b[0], cur});            Pre = cur;    }} return res; }}


Copyright notice: This article Bo Master original articles, blogs, without consent may not be reproduced.

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