Skip to content
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,7 @@
* [KnapsackMemoization](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/dynamicprogramming/KnapsackMemoization.java)
* [LevenshteinDistance](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/dynamicprogramming/LevenshteinDistance.java)
* [LongestAlternatingSubsequence](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/dynamicprogramming/LongestAlternatingSubsequence.java)
* [LongestArithmeticSubsequence](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/dynamicprogramming/LongestArithmeticSubsequence.java)
* [LongestCommonSubsequence](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/dynamicprogramming/LongestCommonSubsequence.java)
* [LongestIncreasingSubsequence](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/dynamicprogramming/LongestIncreasingSubsequence.java)
* [LongestPalindromicSubsequence](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/dynamicprogramming/LongestPalindromicSubsequence.java)
Expand Down Expand Up @@ -733,6 +734,7 @@
* [KnapsackTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/KnapsackTest.java)
* [LevenshteinDistanceTests](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/LevenshteinDistanceTests.java)
* [LongestAlternatingSubsequenceTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/LongestAlternatingSubsequenceTest.java)
* [LongestArithmeticSubsequenceTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/LongestArithmeticSubsequenceTest.java)
* [LongestIncreasingSubsequenceTests](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/LongestIncreasingSubsequenceTests.java)
* [LongestPalindromicSubstringTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/LongestPalindromicSubstringTest.java)
* [LongestValidParenthesesTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/LongestValidParenthesesTest.java)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,12 @@ public ArrayList<Point> getPoints() {
}

/**
* The main divide and conquer, and also recursive algorithm. It gets an
* ArrayList full of points as an argument. If the size of that ArrayList is
* 1 or 2, the ArrayList is returned as it is, or with one less point (if
* the initial size is 2 and one of it's points, is dominated by the other
* one). On the other hand, if the ArrayList's size is bigger than 2, the
* The main divide and conquer, and also recursive algorithm.
* It gets an ArrayList full of points as an argument.
* If the size of that ArrayList is 1 or 2, the ArrayList is
* returned as it is, or with one less point (if the initial size is 2
* and one of it's points, is dominated by the other one).
* On the other hand, if the ArrayList's size is bigger than 2, the
* function is called again, twice, with arguments the corresponding half of
* the initial ArrayList each time. Once the flashback has ended, the
* function produceFinalSkyLine gets called, in order to produce the final
Expand Down
82 changes: 74 additions & 8 deletions src/main/java/com/thealgorithms/others/SkylineProblem.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,61 @@
import java.util.Iterator;
import java.util.Scanner;

/**
* The {@code SkylineProblem} class is used to solve the skyline problem using a
* divide-and-conquer approach.
* It reads input for building data, processes it to find the skyline, and
* prints the skyline.
*/
public class SkylineProblem {

Building[] building;
int count;

/**
* Main function that reads input for the number of buildings and their
* dimensions, computes the skyline using divide-and-conquer,
* and prints the result.
*/
public void run() {
Scanner sc = new Scanner(System.in);

int num = sc.nextInt();
this.building = new Building[num];

// Read the building details and add them to the list of buildings
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();
}

/**
* Adds a building with the given left, height, and right values to the
* buildings list.
*
* @param left The left x-coordinate of the building.
* @param height The height of the building.
* @param right The right x-coordinate of the building.
*/
public void add(int left, int height, int right) {
building[count++] = new Building(left, height, right);
}

/**
* Prints the skyline as a sequence of coordinates and heights.
*
* @param skyline The list of {@link Skyline} objects representing the skyline.
*/
public void print(ArrayList<Skyline> skyline) {
Iterator<Skyline> it = skyline.iterator();

// Print each coordinate and height from the skyline list
while (it.hasNext()) {
Skyline temp = it.next();
System.out.print(temp.coordinates + "," + temp.height);
Expand All @@ -41,29 +68,44 @@ public void print(ArrayList<Skyline> skyline) {
}
}

/**
* Computes the skyline for a range of buildings using the divide-and-conquer
* approach.
*
* @param start The starting index of the buildings to process.
* @param end The ending index of the buildings to process.
* @return A list of {@link Skyline} objects representing the computed skyline.
*/
public ArrayList<Skyline> findSkyline(int start, int end) {
// Base case: only one building, return its skyline.
if (start == end) {
ArrayList<Skyline> list = new ArrayList<>();
list.add(new Skyline(building[start].left, building[start].height));
list.add(new Skyline(building[end].right, 0));

list.add(new Skyline(building[end].right, 0)); // Add the end of the building
return list;
}

int mid = (start + end) / 2;

ArrayList<Skyline> sky1 = this.findSkyline(start, mid);
ArrayList<Skyline> sky2 = this.findSkyline(mid + 1, end);

return this.mergeSkyline(sky1, sky2);
ArrayList<Skyline> sky1 = this.findSkyline(start, mid); // Find the skyline of the left half
ArrayList<Skyline> sky2 = this.findSkyline(mid + 1, end); // Find the skyline of the right half
return this.mergeSkyline(sky1, sky2); // Merge the two skylines
}

/**
* Merges two skylines (sky1 and sky2) into one combined skyline.
*
* @param sky1 The first skyline list.
* @param sky2 The second skyline list.
* @return A list of {@link Skyline} objects representing the merged skyline.
*/
public ArrayList<Skyline> mergeSkyline(ArrayList<Skyline> sky1, ArrayList<Skyline> sky2) {
int currentH1 = 0;
int currentH2 = 0;
ArrayList<Skyline> skyline = new ArrayList<>();
int maxH = 0;

// Merge the two skylines
while (!sky1.isEmpty() && !sky2.isEmpty()) {
if (sky1.get(0).coordinates < sky2.get(0).coordinates) {
int currentX = sky1.get(0).coordinates;
Expand Down Expand Up @@ -96,6 +138,7 @@ public ArrayList<Skyline> mergeSkyline(ArrayList<Skyline> sky1, ArrayList<Skylin
}
}

// Add any remaining points from sky1 or sky2
while (!sky1.isEmpty()) {
skyline.add(sky1.get(0));
sky1.remove(0);
Expand All @@ -109,30 +152,53 @@ public ArrayList<Skyline> mergeSkyline(ArrayList<Skyline> sky1, ArrayList<Skylin
return skyline;
}

/**
* A class representing a point in the skyline with its x-coordinate and height.
*/
public class Skyline {

public int coordinates;
public int height;

/**
* Constructor for the {@code Skyline} class.
*
* @param coordinates The x-coordinate of the skyline point.
* @param height The height of the skyline at the given coordinate.
*/
public Skyline(int coordinates, int height) {
this.coordinates = coordinates;
this.height = height;
}
}

/**
* A class representing a building with its left, height, and right
* x-coordinates.
*/
public class Building {

public int left;
public int height;
public int right;

/**
* Constructor for the {@code Building} class.
*
* @param left The left x-coordinate of the building.
* @param height The height of the building.
* @param right The right x-coordinate of the building.
*/
public Building(int left, int height, int right) {
this.left = left;
this.height = height;
this.right = right;
}
}

/**
* The main method that runs the skyline problem solution.
*
* @param args Command-line arguments (not used).
*/
public static void main(String[] args) {
SkylineProblem skylineProblem = new SkylineProblem();
skylineProblem.run();
Expand Down