diff --git a/Data Structures/Graphs/BFS.java b/Data Structures/Graphs/BFS.java index 4f3bd62d339c..d06a66068632 100644 --- a/Data Structures/Graphs/BFS.java +++ b/Data Structures/Graphs/BFS.java @@ -6,7 +6,7 @@ * @author Unknown * */ -public class bfs{ +public class BFS{ /** * The BFS implemented in code to use. diff --git a/Data Structures/Graphs/DFS.java b/Data Structures/Graphs/DFS.java index 8ceba166e149..747fcbed5dfc 100644 --- a/Data Structures/Graphs/DFS.java +++ b/Data Structures/Graphs/DFS.java @@ -7,7 +7,7 @@ * */ -public class dfs{ +public class DFS{ /** * Implementation in code of a DFS diff --git a/Data Structures/Graphs/PrimMST.java b/Data Structures/Graphs/PrimMST.java index 9d8c2d358c04..25695050275c 100644 --- a/Data Structures/Graphs/PrimMST.java +++ b/Data Structures/Graphs/PrimMST.java @@ -100,7 +100,7 @@ public static void main (String[] args) | / \ | (3)-------(4) 9 */ - MST t = new MST(); + PrimMST t = new PrimMST(); int graph[][] = new int[][] {{0, 2, 0, 6, 0}, {2, 0, 3, 8, 5}, {0, 3, 0, 0, 7}, diff --git a/Dynamic Programming/LevenshteinDistance.java b/Dynamic Programming/LevenshteinDistance.java index 8a5f7249cad9..be0e7c43f112 100644 --- a/Dynamic Programming/LevenshteinDistance.java +++ b/Dynamic Programming/LevenshteinDistance.java @@ -6,7 +6,7 @@ * */ -public class Levenshtein_distance{ +public class LevenshteinDistance{ private static int minimum(int a, int b, int c){ if(a < b && a < c){ return a; diff --git a/Others/SkylineProblem.java b/Others/SkylineProblem.java new file mode 100644 index 000000000000..a0b70631a527 --- /dev/null +++ b/Others/SkylineProblem.java @@ -0,0 +1,131 @@ +import java.util.ArrayList; +import java.util.Iterator; +import java.util.Scanner; + +public class SkylineProblem { + Building[] building; + int count; + + public void run() { + Scanner sc = new Scanner(System.in); + + int num = sc.nextInt(); + this.building = new Building[num]; + + for(int i = 0; i < num; i++) { + String input = sc.next(); + String[] data = input.split(","); + this.add(Integer.parseInt(data[0]), Integer.parseInt(data[1]), Integer.parseInt(data[2])); + } + this.print(this.findSkyline(0, num - 1)); + + sc.close(); + } + + public void add(int left, int height, int right) { + building[count++] = new Building(left, height, right); + } + + public void print(ArrayList skyline) { + Iterator it = skyline.iterator(); + + while(it.hasNext()) { + Skyline temp = it.next(); + System.out.print(temp.coordinates + "," + temp.height); + if(it.hasNext()) { + System.out.print(","); + } + } + + } + + public ArrayList findSkyline(int start, int end) { + if(start == end) { + ArrayList list = new ArrayList<>(); + list.add(new Skyline(building[start].left, building[start].height)); + list.add(new Skyline(building[end].right, 0)); + + return list; + } + + int mid = (start + end) / 2; + + ArrayList sky1 = this.findSkyline(start, mid); + ArrayList sky2 = this.findSkyline(mid + 1, end); + + return this.mergeSkyline(sky1, sky2); + } + + public ArrayList mergeSkyline(ArrayList sky1, ArrayList sky2) { + int currentH1 = 0, currentH2 = 0; + ArrayList skyline = new ArrayList<>(); + int maxH = 0; + + while(!sky1.isEmpty() && !sky2.isEmpty()) { + if(sky1.get(0).coordinates < sky2.get(0).coordinates) { + int currentX = sky1.get(0).coordinates; + currentH1 = sky1.get(0).height; + + if(currentH1 < currentH2) { + sky1.remove(0); + if(maxH != currentH2) skyline.add(new Skyline(currentX, currentH2)); + } else { + maxH = currentH1; + sky1.remove(0); + skyline.add(new Skyline(currentX, currentH1)); + } + } else { + int currentX = sky2.get(0).coordinates; + currentH2 = sky2.get(0).height; + + if(currentH2 < currentH1) { + sky2.remove(0); + if(maxH != currentH1) skyline.add(new Skyline(currentX, currentH1)); + } else { + maxH = currentH2; + sky2.remove(0); + skyline.add(new Skyline(currentX, currentH2)); + } + } + } + + while(!sky1.isEmpty()) { + skyline.add(sky1.get(0)); + sky1.remove(0); + } + + while(!sky2.isEmpty()) { + skyline.add(sky2.get(0)); + sky2.remove(0); + } + + return skyline; + } + + public class Skyline { + public int coordinates; + public int height; + + public Skyline(int coordinates, int height) { + this.coordinates = coordinates; + this.height = height; + } + } + + public class Building { + public int left; + public int height; + public int right; + + public Building(int left, int height, int right) { + this.left = left; + this.height = height; + this.right = right; + } + } + + public static void main(String[] args) { + SkylineProblem skylineProblem = new SkylineProblem(); + skylineProblem.run(); + } +} diff --git a/SkylineProblem/SkylineProblem.java b/SkylineProblem/SkylineProblem.java new file mode 100644 index 000000000000..a0b70631a527 --- /dev/null +++ b/SkylineProblem/SkylineProblem.java @@ -0,0 +1,131 @@ +import java.util.ArrayList; +import java.util.Iterator; +import java.util.Scanner; + +public class SkylineProblem { + Building[] building; + int count; + + public void run() { + Scanner sc = new Scanner(System.in); + + int num = sc.nextInt(); + this.building = new Building[num]; + + for(int i = 0; i < num; i++) { + String input = sc.next(); + String[] data = input.split(","); + this.add(Integer.parseInt(data[0]), Integer.parseInt(data[1]), Integer.parseInt(data[2])); + } + this.print(this.findSkyline(0, num - 1)); + + sc.close(); + } + + public void add(int left, int height, int right) { + building[count++] = new Building(left, height, right); + } + + public void print(ArrayList skyline) { + Iterator it = skyline.iterator(); + + while(it.hasNext()) { + Skyline temp = it.next(); + System.out.print(temp.coordinates + "," + temp.height); + if(it.hasNext()) { + System.out.print(","); + } + } + + } + + public ArrayList findSkyline(int start, int end) { + if(start == end) { + ArrayList list = new ArrayList<>(); + list.add(new Skyline(building[start].left, building[start].height)); + list.add(new Skyline(building[end].right, 0)); + + return list; + } + + int mid = (start + end) / 2; + + ArrayList sky1 = this.findSkyline(start, mid); + ArrayList sky2 = this.findSkyline(mid + 1, end); + + return this.mergeSkyline(sky1, sky2); + } + + public ArrayList mergeSkyline(ArrayList sky1, ArrayList sky2) { + int currentH1 = 0, currentH2 = 0; + ArrayList skyline = new ArrayList<>(); + int maxH = 0; + + while(!sky1.isEmpty() && !sky2.isEmpty()) { + if(sky1.get(0).coordinates < sky2.get(0).coordinates) { + int currentX = sky1.get(0).coordinates; + currentH1 = sky1.get(0).height; + + if(currentH1 < currentH2) { + sky1.remove(0); + if(maxH != currentH2) skyline.add(new Skyline(currentX, currentH2)); + } else { + maxH = currentH1; + sky1.remove(0); + skyline.add(new Skyline(currentX, currentH1)); + } + } else { + int currentX = sky2.get(0).coordinates; + currentH2 = sky2.get(0).height; + + if(currentH2 < currentH1) { + sky2.remove(0); + if(maxH != currentH1) skyline.add(new Skyline(currentX, currentH1)); + } else { + maxH = currentH2; + sky2.remove(0); + skyline.add(new Skyline(currentX, currentH2)); + } + } + } + + while(!sky1.isEmpty()) { + skyline.add(sky1.get(0)); + sky1.remove(0); + } + + while(!sky2.isEmpty()) { + skyline.add(sky2.get(0)); + sky2.remove(0); + } + + return skyline; + } + + public class Skyline { + public int coordinates; + public int height; + + public Skyline(int coordinates, int height) { + this.coordinates = coordinates; + this.height = height; + } + } + + public class Building { + public int left; + public int height; + public int right; + + public Building(int left, int height, int right) { + this.left = left; + this.height = height; + this.right = right; + } + } + + public static void main(String[] args) { + SkylineProblem skylineProblem = new SkylineProblem(); + skylineProblem.run(); + } +}