-
Notifications
You must be signed in to change notification settings - Fork 93
/
SkylineSolution.java
200 lines (164 loc) · 4.61 KB
/
SkylineSolution.java
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
package Coding;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
public class SkylineSolution {
public int greater(int a, int b)
{
if(a > b) return a;
return b;
}
public List<int[]> mergeSkylines(List<int[]> skyLineList_lower, List<int[]> skyLineList_higher)
{
int h1 = 0, h2 = 0;
int newIndex = 0;
List<int[]> skyLine_merged = new ArrayList<int[]>();;
while(true)
{
if(skyLineList_lower.isEmpty() || skyLineList_higher.isEmpty())
{
break;
}
int [] stripe1 = skyLineList_lower.get(0);
int [] stripe2 = skyLineList_higher.get(0);
int [] mergedStripe = new int[2]; // 0: x co-ordinate, 1: height
if(stripe1[0] < stripe2[0]) // comparing x co-ordinates
{
mergedStripe[0] = stripe1[0];
mergedStripe[1] = stripe1[1];
// if 'y' for chosen one is less than last seen height of other skyline
if(stripe1[1] < h2)
{
mergedStripe[1] = h2;
}
// update the last seen height for this skyline
h1 = stripe1[1];
// move to next key point for this skyline
skyLineList_lower.remove(0);
}
else if(stripe2[0] < stripe1[0])
{
mergedStripe[0] = stripe2[0];
mergedStripe[1] = stripe2[1];
if(stripe2[1] < h1)
{
mergedStripe[1] = h1;
}
h2 = stripe2[1];
skyLineList_higher.remove(0);
}
else // (stripe2[0] == stripe1[0]):
{ // In this case, only one point can be added and hence we remove both
mergedStripe[0] = stripe2[0];
mergedStripe[1] = greater(stripe1[1], stripe2[1]);
h1 = stripe1[1];
h2 = stripe2[1];
skyLineList_lower.remove(0);
skyLineList_higher.remove(0);
}
skyLine_merged.add(mergedStripe);
}
if(skyLineList_lower.isEmpty())
{
while(!skyLineList_higher.isEmpty())
{
skyLine_merged.add(skyLineList_higher.remove(0));
}
}
else
{
while(!skyLineList_lower.isEmpty())
{
skyLine_merged.add(skyLineList_lower.remove(0));
}
}
int current = 0;
while(current < skyLine_merged.size())
{
boolean dupeFound = true;
int i = current + 1;
while ((i < skyLine_merged.size()) && dupeFound)
{
if(skyLine_merged.get(current)[1] == skyLine_merged.get(i)[1])
{
dupeFound = true;
skyLine_merged.remove(i);
}
else
{
dupeFound = false;
}
}
current+=1;
}
return skyLine_merged;
}
public List<int[]> getSkyline_rec(int low, int high, int[][]buildings)
{
List<int[]> skyLineList = new ArrayList<int[]>();
if(low > high)
{
return skyLineList;
}
else if(low == high)
{
int x1 = buildings[low][0];
int x2 = buildings[low][1];
int h = buildings[low][2];
int[] element1 = {x1, h};
int[] element2 = {x2, 0};
skyLineList.add(element2);
skyLineList.add(0,element1);
return skyLineList;
}
else
{
int mid = (low + high) / 2;
List<int[]> skyLineList_lower = getSkyline_rec(low, mid, buildings);
List<int[]> skyLineList_higher = getSkyline_rec(mid+1, high, buildings);
return mergeSkylines(skyLineList_lower, skyLineList_higher);
}
}
public List<int[]> getSkyline(int[][] buildings) {
return getSkyline_rec(0, buildings.length-1, buildings);
}
/*
public List<int[]> test(List<int[]> skyLine_merged)
{
int current = 0;
while(current < skyLine_merged.size())
{
if ((current+1) < skyLine_merged.size())
{
if(skyLine_merged.get(current)[1] == skyLine_merged.get(current+1)[1])
{
skyLine_merged.remove(current+1);
}
}
current+=1;
}
return skyLine_merged;
}
*/
public static void main(String[] args)
{
int[][] buildings = {{1,2,1},{1,2,2},{1,2,3}};
// {{1,2,1},{1,2,2},{1,2,3}}
SkylineSolution slnForSkyline = new SkylineSolution();
// List<int[]> skyline_merged = new ArrayList<int[]>();
/*
int [] element = {0,3};
skyline_merged.add(element);
int[] element1 = {2,3};
skyline_merged.add(element1);
int[] element2 = {5,0};
skyline_merged.add(element2);
*/
List<int[]> skyLine = slnForSkyline.getSkyline(buildings);
// List<int[]> solList = slnForSkyline.test(skyline_merged);
for(int i = 0; i < skyLine.size(); i++)
{
System.out.println(skyLine.get(i)[0]+","+skyLine.get(i)[1]);
}
}
}