11package com .thealgorithms .others ;
22
33import java .util .ArrayList ;
4- import java .util .Iterator ;
5- import java .util .Scanner ;
64
5+ /**
6+ * The {@code SkylineProblem} class is used to solve the skyline problem using a
7+ * divide-and-conquer approach.
8+ * It reads input for building data, processes it to find the skyline, and
9+ * prints the skyline.
10+ */
711public class SkylineProblem {
812
913 Building [] building ;
1014 int count ;
1115
12- public void run () {
13- Scanner sc = new Scanner (System .in );
14-
15- int num = sc .nextInt ();
16- this .building = new Building [num ];
17-
18- for (int i = 0 ; i < num ; i ++) {
19- String input = sc .next ();
20- String [] data = input .split ("," );
21- this .add (Integer .parseInt (data [0 ]), Integer .parseInt (data [1 ]), Integer .parseInt (data [2 ]));
22- }
23- this .print (this .findSkyline (0 , num - 1 ));
24-
25- sc .close ();
26- }
27-
16+ /**
17+ * Adds a building with the given left, height, and right values to the
18+ * buildings list.
19+ *
20+ * @param left The left x-coordinate of the building.
21+ * @param height The height of the building.
22+ * @param right The right x-coordinate of the building.
23+ */
2824 public void add (int left , int height , int right ) {
2925 building [count ++] = new Building (left , height , right );
3026 }
3127
32- public void print (ArrayList <Skyline > skyline ) {
33- Iterator <Skyline > it = skyline .iterator ();
34-
35- while (it .hasNext ()) {
36- Skyline temp = it .next ();
37- System .out .print (temp .coordinates + "," + temp .height );
38- if (it .hasNext ()) {
39- System .out .print ("," );
40- }
41- }
42- }
43-
28+ /**
29+ * Computes the skyline for a range of buildings using the divide-and-conquer
30+ * strategy.
31+ *
32+ * @param start The starting index of the buildings to process.
33+ * @param end The ending index of the buildings to process.
34+ * @return A list of {@link Skyline} objects representing the computed skyline.
35+ */
4436 public ArrayList <Skyline > findSkyline (int start , int end ) {
37+ // Base case: only one building, return its skyline.
4538 if (start == end ) {
4639 ArrayList <Skyline > list = new ArrayList <>();
4740 list .add (new Skyline (building [start ].left , building [start ].height ));
48- list .add (new Skyline (building [end ].right , 0 ));
49-
41+ list .add (new Skyline (building [end ].right , 0 )); // Add the end of the building
5042 return list ;
5143 }
5244
5345 int mid = (start + end ) / 2 ;
5446
55- ArrayList <Skyline > sky1 = this .findSkyline (start , mid );
56- ArrayList <Skyline > sky2 = this .findSkyline (mid + 1 , end );
57-
58- return this .mergeSkyline (sky1 , sky2 );
47+ ArrayList <Skyline > sky1 = this .findSkyline (start , mid ); // Find the skyline of the left half
48+ ArrayList <Skyline > sky2 = this .findSkyline (mid + 1 , end ); // Find the skyline of the right half
49+ return this .mergeSkyline (sky1 , sky2 ); // Merge the two skylines
5950 }
6051
52+ /**
53+ * Merges two skylines (sky1 and sky2) into one combined skyline.
54+ *
55+ * @param sky1 The first skyline list.
56+ * @param sky2 The second skyline list.
57+ * @return A list of {@link Skyline} objects representing the merged skyline.
58+ */
6159 public ArrayList <Skyline > mergeSkyline (ArrayList <Skyline > sky1 , ArrayList <Skyline > sky2 ) {
6260 int currentH1 = 0 ;
6361 int currentH2 = 0 ;
6462 ArrayList <Skyline > skyline = new ArrayList <>();
6563 int maxH = 0 ;
6664
65+ // Merge the two skylines
6766 while (!sky1 .isEmpty () && !sky2 .isEmpty ()) {
6867 if (sky1 .get (0 ).coordinates < sky2 .get (0 ).coordinates ) {
6968 int currentX = sky1 .get (0 ).coordinates ;
@@ -96,6 +95,7 @@ public ArrayList<Skyline> mergeSkyline(ArrayList<Skyline> sky1, ArrayList<Skylin
9695 }
9796 }
9897
98+ // Add any remaining points from sky1 or sky2
9999 while (!sky1 .isEmpty ()) {
100100 skyline .add (sky1 .get (0 ));
101101 sky1 .remove (0 );
@@ -109,32 +109,45 @@ public ArrayList<Skyline> mergeSkyline(ArrayList<Skyline> sky1, ArrayList<Skylin
109109 return skyline ;
110110 }
111111
112+ /**
113+ * A class representing a point in the skyline with its x-coordinate and height.
114+ */
112115 public class Skyline {
113-
114116 public int coordinates ;
115117 public int height ;
116118
119+ /**
120+ * Constructor for the {@code Skyline} class.
121+ *
122+ * @param coordinates The x-coordinate of the skyline point.
123+ * @param height The height of the skyline at the given coordinate.
124+ */
117125 public Skyline (int coordinates , int height ) {
118126 this .coordinates = coordinates ;
119127 this .height = height ;
120128 }
121129 }
122130
131+ /**
132+ * A class representing a building with its left, height, and right
133+ * x-coordinates.
134+ */
123135 public class Building {
124-
125136 public int left ;
126137 public int height ;
127138 public int right ;
128139
140+ /**
141+ * Constructor for the {@code Building} class.
142+ *
143+ * @param left The left x-coordinate of the building.
144+ * @param height The height of the building.
145+ * @param right The right x-coordinate of the building.
146+ */
129147 public Building (int left , int height , int right ) {
130148 this .left = left ;
131149 this .height = height ;
132150 this .right = right ;
133151 }
134152 }
135-
136- public static void main (String [] args ) {
137- SkylineProblem skylineProblem = new SkylineProblem ();
138- skylineProblem .run ();
139- }
140153}
0 commit comments