Skip to content

Commit a8e0a48

Browse files
committed
SkylineProblem.java move Others Directory and Refactoring other files
1 parent b5f0c5d commit a8e0a48

File tree

5 files changed

+135
-4
lines changed

5 files changed

+135
-4
lines changed

Data Structures/Graphs/BFS.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* @author Unknown
77
*
88
*/
9-
public class bfs{
9+
public class BFS{
1010

1111
/**
1212
* The BFS implemented in code to use.

Data Structures/Graphs/DFS.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*
88
*/
99

10-
public class dfs{
10+
public class DFS{
1111

1212
/**
1313
* Implementation in code of a DFS

Data Structures/Graphs/PrimMST.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ public static void main (String[] args)
100100
| / \ |
101101
(3)-------(4)
102102
9 */
103-
MST t = new MST();
103+
PrimMST t = new PrimMST();
104104
int graph[][] = new int[][] {{0, 2, 0, 6, 0},
105105
{2, 0, 3, 8, 5},
106106
{0, 3, 0, 0, 7},

Dynamic Programming/LevenshteinDistance.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
*
77
*/
88

9-
public class Levenshtein_distance{
9+
public class LevenshteinDistance{
1010
private static int minimum(int a, int b, int c){
1111
if(a < b && a < c){
1212
return a;

Others/SkylineProblem.java

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
import java.util.ArrayList;
2+
import java.util.Iterator;
3+
import java.util.Scanner;
4+
5+
public class SkylineProblem {
6+
Building[] building;
7+
int count;
8+
9+
public void run() {
10+
Scanner sc = new Scanner(System.in);
11+
12+
int num = sc.nextInt();
13+
this.building = new Building[num];
14+
15+
for(int i = 0; i < num; i++) {
16+
String input = sc.next();
17+
String[] data = input.split(",");
18+
this.add(Integer.parseInt(data[0]), Integer.parseInt(data[1]), Integer.parseInt(data[2]));
19+
}
20+
this.print(this.findSkyline(0, num - 1));
21+
22+
sc.close();
23+
}
24+
25+
public void add(int left, int height, int right) {
26+
building[count++] = new Building(left, height, right);
27+
}
28+
29+
public void print(ArrayList<Skyline> skyline) {
30+
Iterator<Skyline> it = skyline.iterator();
31+
32+
while(it.hasNext()) {
33+
Skyline temp = it.next();
34+
System.out.print(temp.coordinates + "," + temp.height);
35+
if(it.hasNext()) {
36+
System.out.print(",");
37+
}
38+
}
39+
40+
}
41+
42+
public ArrayList<Skyline> findSkyline(int start, int end) {
43+
if(start == end) {
44+
ArrayList<Skyline> list = new ArrayList<>();
45+
list.add(new Skyline(building[start].left, building[start].height));
46+
list.add(new Skyline(building[end].right, 0));
47+
48+
return list;
49+
}
50+
51+
int mid = (start + end) / 2;
52+
53+
ArrayList<Skyline> sky1 = this.findSkyline(start, mid);
54+
ArrayList<Skyline> sky2 = this.findSkyline(mid + 1, end);
55+
56+
return this.mergeSkyline(sky1, sky2);
57+
}
58+
59+
public ArrayList<Skyline> mergeSkyline(ArrayList<Skyline> sky1, ArrayList<Skyline> sky2) {
60+
int currentH1 = 0, currentH2 = 0;
61+
ArrayList<Skyline> skyline = new ArrayList<>();
62+
int maxH = 0;
63+
64+
while(!sky1.isEmpty() && !sky2.isEmpty()) {
65+
if(sky1.get(0).coordinates < sky2.get(0).coordinates) {
66+
int currentX = sky1.get(0).coordinates;
67+
currentH1 = sky1.get(0).height;
68+
69+
if(currentH1 < currentH2) {
70+
sky1.remove(0);
71+
if(maxH != currentH2) skyline.add(new Skyline(currentX, currentH2));
72+
} else {
73+
maxH = currentH1;
74+
sky1.remove(0);
75+
skyline.add(new Skyline(currentX, currentH1));
76+
}
77+
} else {
78+
int currentX = sky2.get(0).coordinates;
79+
currentH2 = sky2.get(0).height;
80+
81+
if(currentH2 < currentH1) {
82+
sky2.remove(0);
83+
if(maxH != currentH1) skyline.add(new Skyline(currentX, currentH1));
84+
} else {
85+
maxH = currentH2;
86+
sky2.remove(0);
87+
skyline.add(new Skyline(currentX, currentH2));
88+
}
89+
}
90+
}
91+
92+
while(!sky1.isEmpty()) {
93+
skyline.add(sky1.get(0));
94+
sky1.remove(0);
95+
}
96+
97+
while(!sky2.isEmpty()) {
98+
skyline.add(sky2.get(0));
99+
sky2.remove(0);
100+
}
101+
102+
return skyline;
103+
}
104+
105+
public class Skyline {
106+
public int coordinates;
107+
public int height;
108+
109+
public Skyline(int coordinates, int height) {
110+
this.coordinates = coordinates;
111+
this.height = height;
112+
}
113+
}
114+
115+
public class Building {
116+
public int left;
117+
public int height;
118+
public int right;
119+
120+
public Building(int left, int height, int right) {
121+
this.left = left;
122+
this.height = height;
123+
this.right = right;
124+
}
125+
}
126+
127+
public static void main(String[] args) {
128+
SkylineProblem skylineProblem = new SkylineProblem();
129+
skylineProblem.run();
130+
}
131+
}

0 commit comments

Comments
 (0)