@@ -21,19 +21,19 @@ public class SkylineDrawing {
2121 * Represents either start or end of building
2222 */
2323 static class BuildingPoint implements Comparable <BuildingPoint > {
24- int point ;
24+ int x ;
2525 boolean isStart ;
2626 int height ;
2727
2828 @ Override
2929 public int compareTo (BuildingPoint o ) {
30- //first compare by point .
30+ //first compare by x .
3131 //If they are same then use this logic
3232 //if two starts are compared then higher height building should be picked first
3333 //if two ends are compared then lower height building should be picked first
3434 //if one start and end is compared then start should appear before end
35- if (this .point != o .point ) {
36- return this .point - o .point ;
35+ if (this .x != o .x ) {
36+ return this .x - o .x ;
3737 } else {
3838 return (this .isStart ? -this .height : this .height ) - (o .isStart ? -o .height : o .height );
3939 }
@@ -47,12 +47,12 @@ public List<int[]> getSkyline(int[][] buildings) {
4747 int index = 0 ;
4848 for (int building [] : buildings ) {
4949 buildingPoints [index ] = new BuildingPoint ();
50- buildingPoints [index ].point = building [0 ];
50+ buildingPoints [index ].x = building [0 ];
5151 buildingPoints [index ].isStart = true ;
5252 buildingPoints [index ].height = building [2 ];
5353
5454 buildingPoints [index + 1 ] = new BuildingPoint ();
55- buildingPoints [index + 1 ].point = building [1 ];
55+ buildingPoints [index + 1 ].x = building [1 ];
5656 buildingPoints [index + 1 ].isStart = false ;
5757 buildingPoints [index + 1 ].height = building [2 ];
5858 index += 2 ;
@@ -62,41 +62,46 @@ public List<int[]> getSkyline(int[][] buildings) {
6262 //using TreeMap because it gives log time performance.
6363 //PriorityQueue in java does not support remove(object) operation in log time.
6464 TreeMap <Integer , Integer > queue = new TreeMap <>();
65+ //PriorityQueue<Integer> queue1 = new PriorityQueue<>(Collections.reverseOrder());
6566 queue .put (0 , 1 );
67+ //queue1.add(0);
6668 int prevMaxHeight = 0 ;
67- List <int []> criticalPoints = new ArrayList <>();
68- for (BuildingPoint cp : buildingPoints ) {
69+ List <int []> result = new ArrayList <>();
70+ for (BuildingPoint buildingPoint : buildingPoints ) {
6971 //if it is start of building then add the height to map. If height already exists then increment
7072 //the value
71- if (cp .isStart ) {
72- queue .compute (cp .height , (key , value ) -> {
73- if (value != null ) {
74- return value + 1 ;
75- }
73+ if (buildingPoint .isStart ) {
74+ queue .compute (buildingPoint .height , (key , value ) -> {
75+ if (value != null ) {
76+ return value + 1 ;
77+ }
7678 return 1 ;
7779 });
80+ // queue1.add(cp.height);
7881 } else { //if it is end of building then decrement or remove the height from map.
79- queue .compute (cp .height , (key , value ) -> {
82+ queue .compute (buildingPoint .height , (key , value ) -> {
8083 if (value == 1 ) {
8184 return null ;
8285 }
8386 return value - 1 ;
8487 });
88+ // queue1.remove(cp.height);
8589 }
86- //peek the current height after addition or removal of building point .
90+ //peek the current height after addition or removal of building x .
8791 int currentMaxHeight = queue .lastKey ();
88- //if height changes from previous height then this building point becomes critcal point.
92+ //int currentMaxHeight = queue1.peek();
93+ //if height changes from previous height then this building x becomes critcal x.
8994 // So add it to the result.
9095 if (prevMaxHeight != currentMaxHeight ) {
91- criticalPoints .add (new int []{cp . point , currentMaxHeight });
96+ result .add (new int []{buildingPoint . x , currentMaxHeight });
9297 prevMaxHeight = currentMaxHeight ;
9398 }
9499 }
95- return criticalPoints ;
100+ return result ;
96101 }
97102
98103 public static void main (String args []) {
99- int [][] buildings = {{0 ,3 ,3 },{1 , 5 , 3 },{2 ,4 , 3 },{3 , 7 , 3 }};
104+ int [][] buildings = {{1 ,3 ,4 },{3 , 4 , 4 },{2 ,6 , 2 },{8 , 11 , 4 }, { 7 , 9 , 3 },{ 10 , 11 , 2 }};
100105 SkylineDrawing sd = new SkylineDrawing ();
101106 List <int []> criticalPoints = sd .getSkyline (buildings );
102107 criticalPoints .forEach (cp -> System .out .println (cp [0 ] + " " + cp [1 ]));
0 commit comments