You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Given N buildings in a x-axis,each building is a rectangle and can be represented by a triple (start, end, height),where start is the start position on x-axis, end is the end position on x-axis and height is the height of the building. Buildings may overlap if you see them from far away,find the outline of them。
3
-
An outline can be represented by a triple, (start, end, height), where start is the start position on x-axis of the outline, end is the end position on x-axis and height is the height of the outline.
19
+
LintCode description:
20
+
Given N buildings in a x-axis,each building is a rectangle and can be represented by a triple (start, end, height),
21
+
where start is the start position on x-axis, end is the end position on x-axis and height is the height of the building.
22
+
Buildings may overlap if you see them from far away,find the outline of them。
23
+
An outline can be represented by a triple, (start, end, height),
24
+
where start is the start position on x-axis of the outline,
25
+
end is the end position on x-axis and height is the height of the outline.
26
+
4
27
Example
5
28
Given 3 buildings:
6
29
[
@@ -19,32 +42,114 @@
19
42
Tags Expand
20
43
LintCode Copyright Heap
21
44
*/
45
+
46
+
/*LeetCode description
47
+
A city's skyline is the outer contour of the silhouette formed by all the buildings in that city when viewed from a distance. Now suppose you are given the locations and height of all the buildings as shown on a cityscape photo (Figure A), write a program to output the skyline formed by these buildings collectively (Figure B).
48
+
49
+
Buildings Skyline Contour
50
+
The geometric information of each building is represented by a triplet of integers [Li, Ri, Hi], where Li and Ri are the x coordinates of the left and right edge of the ith building, respectively, and Hi is its height. It is guaranteed that 0 ≤ Li, Ri ≤ INT_MAX, 0 < Hi ≤ INT_MAX, and Ri - Li > 0. You may assume all buildings are perfect rectangles grounded on an absolutely flat surface at height 0.
51
+
52
+
For instance, the dimensions of all buildings in Figure A are recorded as: [ [2 9 10], [3 7 15], [5 12 12], [15 20 10], [19 24 8] ] .
53
+
54
+
The output is a list of "key points" (red dots in Figure B) in the format of [ [x1,y1], [x2, y2], [x3, y3], ... ] that uniquely defines a skyline. A key point is the left endpoint of a horizontal line segment. Note that the last key point, where the rightmost building ends, is merely used to mark the termination of the skyline, and always has zero height. Also, the ground in between any two adjacent buildings should be considered part of the skyline contour.
55
+
56
+
For instance, the skyline in Figure B should be represented as:[ [2 10], [3 15], [7 12], [12 0], [15 10], [20 8], [24, 0] ].
57
+
58
+
Notes:
59
+
60
+
The number of buildings in any input list is guaranteed to be in the range [0, 10000].
61
+
The input list is already sorted in ascending order by the left x position Li.
62
+
The output list must be sorted by the x position.
63
+
There must be no consecutive horizontal lines of equal height in the output skyline. For instance, [...[2 3], [4 5], [7 5], [11 5], [12 7]...] is not acceptable; the three lines of height 5 should be merged into one in the final output as such: [...[2 3], [4 5], [12 7], ...]
64
+
65
+
Hide Tags Divide and Conquer Binary Indexed Tree Heap Segment Tree
66
+
67
+
*/
68
+
22
69
/*
23
-
Thoughts:
24
-
Well, based on JiuZhang, http://www.jiuzhang.com/solutions/building-outline/, implement a HashHeap.
25
-
**HashHeap. Super long implementation: http://www.jiuzhang.com/solutions/hash-heap/
70
+
Thoughts
71
+
Based idea here:http://codechen.blogspot.com/2015/06/leetcode-skyline-problem.html?_sm_au_=isVmHvFmFs40TWRt
72
+
Here is the thinking process, 3.15.2016
73
+
74
+
The class HeightPoint{int x, int height} is very similar to scan line Point{int x, int flag}. However the usage is a bit different.
75
+
Sort all of the heightPoints by index && by height.
76
+
Use nagtive height for start point to make sure start appears before end point, tho they share same height
77
+
We use an extra priorityQueue to store the height being processed (note, this queue, decending order, max in front)
78
+
When having a negative height(start point), add into queue
79
+
At each point, find heightest point (common sense!) and mark it on map: the overlapping point at this index will be skipped because the rest are not high enough.
80
+
How to process the rest redundant point?
81
+
Here introduce a 'prev' variable that stores last processed value. If same height appears right before curr, don't add to result; it's added during this continuous line.
82
+
How to maintain the queue?
83
+
Once a height > 0 appears, remove that height from queue.
84
+
OKay, let's break it down:
85
+
because we sort HeightPoint object by index and height, start height will appear before end height of same building, for sure.
86
+
So whenever positive height appears, the same bulding must have been marked, so can safely remove the height instance from queue.
87
+
Well, in HeightPoint object, start height is negative for sorting purpose. When adding into queue, use it's absolute value : )
0 commit comments