GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — Java/Others/SkylineProblem.java at master · whyhe/Java Expand file tree/ Copy pathSkylineProblem.javaMore file actions 133 lines (106 loc) · 3.77 KB / Copy pathSkylineProblem.javaFile metadata and controls133 lines (106 loc) · 3.77 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 package Others; 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> skyline) { Iterator<Skyline> 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<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)); 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); } public ArrayList<Skyline> mergeSkyline(ArrayList<Skyline> sky1, ArrayList<Skyline> sky2) { int currentH1 = 0, currentH2 = 0; ArrayList<Skyline> 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(); } } |
| You are visitor 01402775 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |