LeetCode The Skyline Problem
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).
Buildings Skyline Contour
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.
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] ] .
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.
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] ].
Notes:
The number of buildings in any input list is guaranteed to be in the range [0, 10000].
The input list is already sorted in ascending order by the left x position Li.
The output list must be sorted by the x position.
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], ...]
找个个discuss里用优先队列的,但是感觉代码写的非常凶险(miao)而且运行时间也比较长,后面又找了个用分治的感觉无论从理解还是实现角度都要好很多。
class Solution {
public:
vector<pair<int, int>> getSkyline(vector<vector<int>>& buildings) {
vector<pair<int, int> > res;
priority_queue<long> hs;
int len = buildings.size();
int pos = 0;
int curx= -1;
while (pos < len || !hs.empty()) {
curx = hs.empty() ? buildings[pos][0] : endX(hs.top());
if (pos >= len || buildings[pos][0] > curx) {
while (!hs.empty() && curx >= endX(hs.top())) hs.pop();
} else {
curx = buildings[pos][0];
while (pos < len && curx == buildings[pos][0]) {
hs.push(val(buildings[pos][2], buildings[pos][1]));
pos++;
}
}
int h = hs.empty() ? 0 : height(hs.top());
if (res.empty() || res.back().second != h) {
res.push_back(make_pair(curx, h));
}
}
return res;
}
long val(long h, long e) {
return (h<<32) | e;
}
long height(long v) {
return v>>32;
}
long endX(long v) {
return v & 0xffffffff;
}
};
下面这个是递归版本的,用C++实现好像比其他语言慢很多,不知道为什么
class Solution {
public:
vector<pair<int, int>> getSkyline(vector<vector<int>>& buildings) {
return dfs(buildings, 0, buildings.size());
}
vector<pair<int, int> > dfs(vector<vector<int> >& bs, int start, int end) {
vector<pair<int, int> > res;
if (start >= end) {
return res;
}
if (start + 1 == end) {
res.push_back(make_pair(bs[start][0], bs[start][2]));
res.push_back(make_pair(bs[start][1], 0));
return res;
}
int div = (start + end) / 2;
vector<pair<int, int> > lres = dfs(bs, start, div);
vector<pair<int, int> > rres = dfs(bs, div, end);
return merge(lres, rres);
}
vector<pair<int, int> > merge(vector<pair<int, int> >& lres, vector<pair<int, int> >& rres) {
vector<pair<int, int> > res;
int llen = lres.size();
int rlen = rres.size();
int lh = 0, rh = 0, h = 0;
int lpos = 0, rpos = 0;
pair<int, int> item;
while (lpos < llen && rpos < rlen) {
int lx = lres[lpos].first;
int rx = rres[rpos].first;
if (lx < rx) {
lh = lres[lpos].second;
item.first = lx;
lpos++;
} else if (lx > rx) {
rh = rres[rpos].second;
item.first = rx;
rpos++;
} else {
lh = lres[lpos].second;
rh = rres[rpos].second;
item.first = lx;
lpos++, rpos++;
}
item.second = max(lh, rh);
if (res.empty() || res.back().second != item.second) {
res.push_back(item);
}
}
while (lpos < llen) {
if (res.empty() || res.back().second != lres[lpos].second) {
res.push_back(lres[lpos]);
}
lpos++;
}
while (rpos < rlen) {
if (res.empty() || res.back().second != rres[rpos].second) {
res.push_back(rres[rpos]);
}
rpos++;
}
return res;
}
};
比如下面这个python版本的只要170+ms,c++的那个就是根据这个改的:
class Solution:
# @param {integer[][]} buildings
# @return {integer[][]}
def getSkyline(self, buildings):
if buildings==[]:
return []
if len(buildings)==1:
return [[buildings[0][0],buildings[0][2]],[buildings[0][1],0]]
mid=(len(buildings)-1)/2
left=self.getSkyline(buildings[0:mid+1])
right=self.getSkyline(buildings[mid+1:])
return self.merge(left,right)
def merge(self,left,right):
i=0
j=0
result=[]
h1=None
h2=None
while i<len(left) and j<len(right):
if left[i][0]<right[j][0]:
h1=left[i][1]
new=[left[i][0],max(h1,h2)]
if result==[] or result[-1][1]!=new[1]:
result.append(new)
i+=1
elif left[i][0]>right[j][0]:
h2=right[j][1]
new=[right[j][0],max(h1,h2)]
if result==[] or result[-1][1]!=new[1]:
result.append(new)
j+=1
else:
h1=left[i][1]
h2=right[j][1]
new=[right[j][0],max(h1,h2)]
if result==[] or result[-1][1]!=new[1]:
result.append([right[j][0],max(h1,h2)])
i+=1
j+=1
while i<len(left):
if result==[] or result[-1][1]!=left[i][1]:
result.append(left[i][:])
i+=1
while j<len(right):
if result==[] or result[-1][1]!=right[j][1]:
result.append(right[j][:])
j+=1
return result