1
1
package com .thealgorithms .others ;
2
2
3
3
import java .util .ArrayList ;
4
- import java .util .Iterator ;
5
- import java .util .Scanner ;
6
4
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
+ */
7
11
public class SkylineProblem {
8
12
9
13
Building [] building ;
10
14
int count ;
11
15
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
+ */
28
24
public void add (int left , int height , int right ) {
29
25
building [count ++] = new Building (left , height , right );
30
26
}
31
27
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
+ */
44
36
public ArrayList <Skyline > findSkyline (int start , int end ) {
37
+ // Base case: only one building, return its skyline.
45
38
if (start == end ) {
46
39
ArrayList <Skyline > list = new ArrayList <>();
47
40
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
50
42
return list ;
51
43
}
52
44
53
45
int mid = (start + end ) / 2 ;
54
46
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
59
50
}
60
51
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
+ */
61
59
public ArrayList <Skyline > mergeSkyline (ArrayList <Skyline > sky1 , ArrayList <Skyline > sky2 ) {
62
60
int currentH1 = 0 ;
63
61
int currentH2 = 0 ;
64
62
ArrayList <Skyline > skyline = new ArrayList <>();
65
63
int maxH = 0 ;
66
64
65
+ // Merge the two skylines
67
66
while (!sky1 .isEmpty () && !sky2 .isEmpty ()) {
68
67
if (sky1 .get (0 ).coordinates < sky2 .get (0 ).coordinates ) {
69
68
int currentX = sky1 .get (0 ).coordinates ;
@@ -96,6 +95,7 @@ public ArrayList<Skyline> mergeSkyline(ArrayList<Skyline> sky1, ArrayList<Skylin
96
95
}
97
96
}
98
97
98
+ // Add any remaining points from sky1 or sky2
99
99
while (!sky1 .isEmpty ()) {
100
100
skyline .add (sky1 .get (0 ));
101
101
sky1 .remove (0 );
@@ -109,32 +109,45 @@ public ArrayList<Skyline> mergeSkyline(ArrayList<Skyline> sky1, ArrayList<Skylin
109
109
return skyline ;
110
110
}
111
111
112
+ /**
113
+ * A class representing a point in the skyline with its x-coordinate and height.
114
+ */
112
115
public class Skyline {
113
-
114
116
public int coordinates ;
115
117
public int height ;
116
118
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
+ */
117
125
public Skyline (int coordinates , int height ) {
118
126
this .coordinates = coordinates ;
119
127
this .height = height ;
120
128
}
121
129
}
122
130
131
+ /**
132
+ * A class representing a building with its left, height, and right
133
+ * x-coordinates.
134
+ */
123
135
public class Building {
124
-
125
136
public int left ;
126
137
public int height ;
127
138
public int right ;
128
139
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
+ */
129
147
public Building (int left , int height , int right ) {
130
148
this .left = left ;
131
149
this .height = height ;
132
150
this .right = right ;
133
151
}
134
152
}
135
-
136
- public static void main (String [] args ) {
137
- SkylineProblem skylineProblem = new SkylineProblem ();
138
- skylineProblem .run ();
139
- }
140
153
}
0 commit comments