JavaAlgorithms/Others/SkylineProblem.java

133 lines
3.3 KiB
Java
Raw Normal View History

package Others;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Scanner;
public class SkylineProblem {
2020-10-24 18:23:28 +08:00
Building[] building;
int count;
2020-10-24 18:23:28 +08:00
public void run() {
Scanner sc = new Scanner(System.in);
2020-10-24 18:23:28 +08:00
int num = sc.nextInt();
this.building = new Building[num];
2020-10-24 18:23:28 +08:00
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]));
}
2020-10-24 18:23:28 +08:00
this.print(this.findSkyline(0, num - 1));
2020-10-24 18:23:28 +08:00
sc.close();
}
2020-10-24 18:23:28 +08:00
public void add(int left, int height, int right) {
building[count++] = new Building(left, height, right);
}
2020-10-24 18:23:28 +08:00
public void print(ArrayList<Skyline> skyline) {
Iterator<Skyline> it = skyline.iterator();
2020-10-24 18:23:28 +08:00
while (it.hasNext()) {
Skyline temp = it.next();
System.out.print(temp.coordinates + "," + temp.height);
if (it.hasNext()) {
System.out.print(",");
}
}
2020-10-24 18:23:28 +08:00
}
2020-10-24 18:23:28 +08:00
public ArrayList<Skyline> findSkyline(int start, int end) {
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));
2020-10-24 18:23:28 +08:00
return list;
}
2020-10-24 18:23:28 +08:00
int mid = (start + end) / 2;
2020-10-24 18:23:28 +08:00
ArrayList<Skyline> sky1 = this.findSkyline(start, mid);
ArrayList<Skyline> sky2 = this.findSkyline(mid + 1, end);
2020-10-24 18:23:28 +08:00
return this.mergeSkyline(sky1, sky2);
}
2020-10-24 18:23:28 +08:00
public ArrayList<Skyline> mergeSkyline(ArrayList<Skyline> sky1, ArrayList<Skyline> sky2) {
int currentH1 = 0, currentH2 = 0;
ArrayList<Skyline> skyline = new ArrayList<>();
int maxH = 0;
2020-10-24 18:23:28 +08:00
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;
2020-10-24 18:23:28 +08:00
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));
}
2020-10-24 18:23:28 +08:00
} 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));
}
}
}
2020-10-24 18:23:28 +08:00
while (!sky1.isEmpty()) {
skyline.add(sky1.get(0));
sky1.remove(0);
}
2020-10-24 18:23:28 +08:00
while (!sky2.isEmpty()) {
skyline.add(sky2.get(0));
sky2.remove(0);
}
2020-10-24 18:23:28 +08:00
return skyline;
}
2020-10-24 18:23:28 +08:00
public class Skyline {
public int coordinates;
public int height;
public Skyline(int coordinates, int height) {
this.coordinates = coordinates;
this.height = height;
}
2020-10-24 18:23:28 +08:00
}
2020-10-24 18:23:28 +08:00
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;
}
2020-10-24 18:23:28 +08:00
}
public static void main(String[] args) {
SkylineProblem skylineProblem = new SkylineProblem();
skylineProblem.run();
}
}