Skip to content

Commit 67da3bf

Browse files
author
Tushar Roy
committed
update in skyline
1 parent e701dcd commit 67da3bf

2 files changed

Lines changed: 28 additions & 23 deletions

File tree

‎python/geometry/skylinedrawing.py‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def get_skyline(buildings):
3535
queue = {}
3636
queue[0] = 1
3737
prev_max_height = 0
38-
critical_points = []
38+
result = []
3939
for building_point in building_points:
4040
if building_point.is_start:
4141
if building_point.height in queue:
@@ -52,11 +52,11 @@ def get_skyline(buildings):
5252
current_max_height = max(queue.keys())
5353

5454
if prev_max_height != current_max_height:
55-
critical_points.append([building_point.point, current_max_height])
55+
result.append([building_point.point, current_max_height])
5656
prev_max_height = current_max_height
57-
return critical_points
57+
return result
5858

5959
if __name__ == '__main__':
60-
buildings = [[0, 3, 3], [1, 5, 3], [2, 4, 3], [3, 7, 3]]
60+
buildings = [[1, 3, 4], [3, 4, 4], [2, 6, 2], [8, 11, 4], [7, 9, 3], [10, 11, 2]]
6161
print(get_skyline(buildings))
6262

‎src/com/interview/geometry/SkylineDrawing.java‎

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,19 @@ public class SkylineDrawing {
2121
* Represents either start or end of building
2222
*/
2323
static class BuildingPoint implements Comparable<BuildingPoint> {
24-
int point;
24+
int x;
2525
boolean isStart;
2626
int height;
2727

2828
@Override
2929
public int compareTo(BuildingPoint o) {
30-
//first compare by point.
30+
//first compare by x.
3131
//If they are same then use this logic
3232
//if two starts are compared then higher height building should be picked first
3333
//if two ends are compared then lower height building should be picked first
3434
//if one start and end is compared then start should appear before end
35-
if (this.point != o.point) {
36-
return this.point - o.point;
35+
if (this.x != o.x) {
36+
return this.x - o.x;
3737
} else {
3838
return (this.isStart ? -this.height : this.height) - (o.isStart ? -o.height : o.height);
3939
}
@@ -47,12 +47,12 @@ public List<int[]> getSkyline(int[][] buildings) {
4747
int index = 0;
4848
for(int building[] : buildings) {
4949
buildingPoints[index] = new BuildingPoint();
50-
buildingPoints[index].point = building[0];
50+
buildingPoints[index].x = building[0];
5151
buildingPoints[index].isStart = true;
5252
buildingPoints[index].height = building[2];
5353

5454
buildingPoints[index + 1] = new BuildingPoint();
55-
buildingPoints[index + 1].point = building[1];
55+
buildingPoints[index + 1].x = building[1];
5656
buildingPoints[index + 1].isStart = false;
5757
buildingPoints[index + 1].height = building[2];
5858
index += 2;
@@ -62,41 +62,46 @@ public List<int[]> getSkyline(int[][] buildings) {
6262
//using TreeMap because it gives log time performance.
6363
//PriorityQueue in java does not support remove(object) operation in log time.
6464
TreeMap<Integer, Integer> queue = new TreeMap<>();
65+
//PriorityQueue<Integer> queue1 = new PriorityQueue<>(Collections.reverseOrder());
6566
queue.put(0, 1);
67+
//queue1.add(0);
6668
int prevMaxHeight = 0;
67-
List<int[]> criticalPoints = new ArrayList<>();
68-
for(BuildingPoint cp : buildingPoints) {
69+
List<int[]> result = new ArrayList<>();
70+
for(BuildingPoint buildingPoint : buildingPoints) {
6971
//if it is start of building then add the height to map. If height already exists then increment
7072
//the value
71-
if (cp.isStart) {
72-
queue.compute(cp.height, (key, value) -> {
73-
if (value != null) {
74-
return value + 1;
75-
}
73+
if (buildingPoint.isStart) {
74+
queue.compute(buildingPoint.height, (key, value) -> {
75+
if (value != null) {
76+
return value + 1;
77+
}
7678
return 1;
7779
});
80+
// queue1.add(cp.height);
7881
} else { //if it is end of building then decrement or remove the height from map.
79-
queue.compute(cp.height, (key, value) -> {
82+
queue.compute(buildingPoint.height, (key, value) -> {
8083
if (value == 1) {
8184
return null;
8285
}
8386
return value - 1;
8487
});
88+
// queue1.remove(cp.height);
8589
}
86-
//peek the current height after addition or removal of building point.
90+
//peek the current height after addition or removal of building x.
8791
int currentMaxHeight = queue.lastKey();
88-
//if height changes from previous height then this building point becomes critcal point.
92+
//int currentMaxHeight = queue1.peek();
93+
//if height changes from previous height then this building x becomes critcal x.
8994
// So add it to the result.
9095
if (prevMaxHeight != currentMaxHeight) {
91-
criticalPoints.add(new int[]{cp.point, currentMaxHeight});
96+
result.add(new int[]{buildingPoint.x, currentMaxHeight});
9297
prevMaxHeight = currentMaxHeight;
9398
}
9499
}
95-
return criticalPoints;
100+
return result;
96101
}
97102

98103
public static void main(String args[]) {
99-
int [][] buildings = {{0,3,3},{1,5,3},{2,4,3},{3,7,3}};
104+
int [][] buildings = {{1,3,4},{3,4,4},{2,6,2},{8,11,4}, {7,9,3},{10,11,2}};
100105
SkylineDrawing sd = new SkylineDrawing();
101106
List<int[]> criticalPoints = sd.getSkyline(buildings);
102107
criticalPoints.forEach(cp -> System.out.println(cp[0] + " " + cp[1]));

0 commit comments

Comments
 (0)