Title: The topic is too long, see link--> The Skyline problem
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], ...]
Test instructions: Record the inflection point of each building, continuous the same height, only record the front.
Idea: each vertical bar in order to deposit, for each rectangle, have a height of the same two vertical lines, in order to in a single scan, you can add all the vertical bar, using a small trick, the right edge of the height of the negative, just can be labeled. will be deposited in the list sort, need to customize a comparator comparison a[],b[], if a[0] = = b[0] Description Vertical line coincident, this time should be high in front, if a[0]! = b[0] Just follow a[0]b[0] from small to large.
Finally, when traversing the list, in order to satisfy the note4, that is, the same height is only recorded at the top, it is necessary to use the pre and cur respectively to record the current highest and last highest. At the time of judgment, need to maintain each current maximum value, can use Priorityqueue weight queue, its construction principle is the big top heap, namely the root node is the smallest (or maximum) two fork tree, the default is the minimum value, according to test instructions, the subject needs to define a comparator, the root save the current maximum value ( Note that I am talking about the same concept in the pile team, in order to facilitate understanding, different times with different statements.
The core of the idea: Scan to the left edge, will be added to the big top heap, the value of cur to peek is the current maximum value, when the cur and the pre is different, the coordinates are added to the result queue, when the right edge (negative height), prove that the rectangle has reached the end, in the heap to remove its height value, If the queue is empty at this point, it proves that this is not contiguous, that is, the height to be joined at this time is 0, if not empty, then update the corresponding current maximum.
Code:
Public classSolution {//large top Push comparator Public classMaxcomImplementsComparator<integer> { Public intCompare (integer A, integer b) {returnB-a;//big at the top of the heap } } //Array comparators Public classArraycomImplementscomparator<int[]> { Public intCompareint[] A,int[] b) {if(a[0]! = b[0])returnA[0]-b[0];//Sort by the left boundary first returnB[1]-a[1];//The same is higher in the front } } Publiclist<int[]> Getskyline (int[] buildings) {List<int[]> res =Newarraylist<int[]>(); Priorityqueue<Integer> maxheap =NewPriorityqueue<integer> (11,Newmaxcom ()); List<int[]> ver =Newarraylist<int[]> ();//record each vertical line for(inti = 0; i < buildings.length; i++){ int[] temp =Buildings[i]; Ver.add (New int[]{temp[0], temp[2]});//left Border vertical lineVer.add (New int[]{temp[1],-temp[2]});//right Border vertical line to distinguish negative values} collections.sort (ver,Newarraycom ()); intCur = 0, pre = 0; for(inti = 0; I < ver.size (); i++){ int[] temp =Ver.get (i); if(Temp[1] > 0) {//left BorderMaxheap.offer (temp[1]);//Highly queuedcur = Maxheap.peek ();//the highest current}Else{//Right BorderMaxheap.remove (-temp[1]);//remove the corresponding height from the heap here is the convenience of keeping negative values on the rightCur = (Maxheap.peek () = =NULL? 0:maxheap.peek ());//If the right boundary is the last one then the height is 0, otherwise the current highest is updated } if(cur! = Pre) {//not equal to the last highestRes.add (New int[]{temp[0], cur}); Pre= cur;//Save the current height for the next front height } } returnRes; }}
Reference Link: http://blog.csdn.net/xudli/article/details/46349383
[Leetcode-java] The Skyline problem