|
| 1 | +/*Copyright (c) Feb 22, 2016 CareerMonk Publications and others. |
| 2 | + * E-Mail : info@careermonk.com |
| 3 | + * Creation Date : 2015-01-10 06:15:46 |
| 4 | + * Last modification : 2006-05-31 |
| 5 | + by : Narasimha Karumanchi |
| 6 | + * File Name : SkylinesDivideandConquer.java |
| 7 | + * Book Title : Data Structures And Algorithms Made In Java |
| 8 | + * Warranty : This software is provided "as is" without any |
| 9 | + * warranty; without even the implied warranty of |
| 10 | + * merchantability or fitness for a particular purpose. |
| 11 | + * |
| 12 | + */ |
| 13 | + |
| 14 | + |
| 15 | +package chapter18divideandconquer; |
| 16 | +class Building{ |
| 17 | + int left, right, h; |
| 18 | + public Building(int x1, int h1, int x2) { left = x1; h = h1; right = x2; } |
| 19 | +} |
| 20 | +class strip{ |
| 21 | + int left, h; |
| 22 | + strip(int x1, int h1) { left = x1; h = h1; } |
| 23 | + } |
| 24 | +class Skyline{ |
| 25 | + strip[] strips; |
| 26 | + public int Count; |
| 27 | + int StartLoc; |
| 28 | + public Skyline(int n) { |
| 29 | + Count = 0; StartLoc = 0; strips = new strip[n]; |
| 30 | + } |
| 31 | + public void Append(strip str) { |
| 32 | + strips[StartLoc+Count] = str; Count++; |
| 33 | + } |
| 34 | + public strip Head() { |
| 35 | + return strips[StartLoc]; |
| 36 | + } |
| 37 | + public strip RemoveHead(){ |
| 38 | + strip str = strips[StartLoc]; |
| 39 | + Count--; StartLoc++; |
| 40 | + return str; |
| 41 | + } |
| 42 | + public String ToString(){ |
| 43 | + String str = ""; |
| 44 | + for(int i = StartLoc; i < StartLoc+Count; i++){ |
| 45 | + if (i > StartLoc) |
| 46 | + str = str + ","; |
| 47 | + str = str + strips[i].left + "," + strips[i].h; |
| 48 | + } |
| 49 | + return "(" + str + ")"; |
| 50 | + } |
| 51 | + } |
| 52 | +public class SkylinesDivideandConquer { |
| 53 | + static Skyline GetSkyline(Building[] buildings, int start, int end){ |
| 54 | + if (start == end){ |
| 55 | + Skyline skl = new Skyline(2); |
| 56 | + skl.Append(new strip(buildings[start].left, buildings[start].h) ); |
| 57 | + skl.Append(new strip(buildings[start].right, 0)); |
| 58 | + return skl; |
| 59 | + } |
| 60 | + int mid = (start+end)/2; |
| 61 | + Skyline sk1 = GetSkyline(buildings, start, mid); |
| 62 | + Skyline sk2 = GetSkyline(buildings, mid+1, end); |
| 63 | + return MergeSkylines(sk1, sk2); |
| 64 | + } |
| 65 | + static Skyline MergeSkylines(Skyline SK1, Skyline SK2){ |
| 66 | + Skyline newSkl = new Skyline(SK1.Count + SK2.Count); // Allocate array space |
| 67 | + int currentHeight1 = 0; int currentHeight2 = 0; |
| 68 | + while ((SK1.Count > 0) && (SK2.Count > 0)) |
| 69 | + if (SK1.Head().left < SK2.Head().left){ |
| 70 | + int CurX = SK1.Head().left; |
| 71 | + currentHeight1 = SK1.Head().h; |
| 72 | + int MaxH = currentHeight1; |
| 73 | + if (currentHeight2 > MaxH) MaxH = currentHeight2; |
| 74 | + newSkl.Append(new strip(CurX, MaxH)); |
| 75 | + SK1.RemoveHead(); |
| 76 | + } |
| 77 | + else{ |
| 78 | + int CurX = SK2.Head().left; |
| 79 | + currentHeight2 = SK2.Head().h; |
| 80 | + int MaxH = currentHeight1; |
| 81 | + if (currentHeight2 > MaxH) MaxH = currentHeight2; |
| 82 | + newSkl.Append(new strip(CurX, MaxH)); |
| 83 | + SK2.RemoveHead(); |
| 84 | + } |
| 85 | + while (SK1.Count > 0){ |
| 86 | + strip str = SK1.RemoveHead(); newSkl.Append(str); |
| 87 | + } |
| 88 | + while (SK2.Count > 0){ |
| 89 | + strip str = SK2.RemoveHead(); newSkl.Append(str); |
| 90 | + } |
| 91 | + return newSkl; |
| 92 | + } |
| 93 | +} |
0 commit comments