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).
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], ...]
我的解法:
bool building_cmp_less (const vector<int>& b1, const vector<int>& b2) {
return b1[0] < b2[0];
}
class Solution {
public:
vector<vector<int>> getSkyline(vector<vector<int>>& buildings) {
int len_v1 = buildings.size();
if (len_v1 == 0) return {};
for (int i=0; i<len_v1; ++i) {
int R1 = buildings[i][1];
buildings.emplace_back(vector<int>({R1, R1, 0}));
for (int j=i+1; j<len_v1 and R1<buildings[j][0]; ++j) {
int R2 = buildings[j][1];
if (R1 > R2) {
buildings.emplace_back(vector<int>({R2, R2, buildings[i][2]}));
}
}
}
sort(buildings.begin(), buildings.end(), building_cmp_less);
int len_v2 = buildings.size();
for (int i=0; i<len_v2; ++i) {
int L1 = buildings[i][0];
int H1 = buildings[i][2];
for (int j=0; j<i; ++j) {
int L2 = buildings[j][0];
int R2 = buildings[j][1];
int H2 = buildings[j][2];
if (L1>L2 and L1<R2 and H1<H2) {
L1 = max(L1, R2);
}
if (L1==R2 and H1==H2) {
}
}
buildings[i][0] = L1;
}
vector<vector<int>> ret = {{buildings[0][0], buildings[0][2]}};
for (int i=1; i<len_v2; ++i) {
int L1 = buildings[i][0];
int R1 = buildings[i][1];
if (L1 > R1) continue;
int H1 = buildings[i][2];
int L2 = (*ret.rbegin())[0];
int H2 = (*ret.rbegin())[1];
if (L1==L2) {
if (H1>H2) (*ret.rbegin())[1] = H1;
}
else {
ret.emplace_back(vector<int>({L1, H1}));
}
}
return ret;
}
};
第13个例子过不了了,需要加的补丁更多了
看答案:https://zxi.mytechroad.com/blog/tree/leetcode-218-the-skyline-problem/
扫描线sweeping line method
基本思想:进入的时候选最大的,离开的时候选第二大的
关键点:每条竖线如何高效判断其他交点。brute force肯定不好。下面伪代码的意思是,把还有效的block的高度都存在一个数据结构里,这样就必然都有交点!
我的模仿默写:
class Solution {
public:
vector<vector<int>> getSkyline(vector<vector<int>>& buildings) {
typedef pair<int,int> Event;
vector<Event> events;
for (auto b:buildings) {
events.push_back({b[0], b[2]});
events.push_back({b[1], -b[2]});
}
sort(events.begin(), events.end(), [](const Event& e1, const Event& e2) -> bool {
if (e1.first == e2.first) return e1.second > e2.second;
return e1.first < e2.first;
});
vector<vector<int>> ans;
for (auto e:events) {
int x = e.first;
bool entering = (e.second > 0);
int h = abs(e.second);
if (entering) {
if (h > MaxHeight()) {
ans.push_back({x, h});
}
heights.insert(h);
}
else {
heights.erase(heights.equal_range(h).first); // 为什么是erase first呢,确实只需要删一个,但是为什么是删第一个?
if (h > MaxHeight()) { // 忘了写这行,
ans.push_back({x, MaxHeight()});
}
}
}
return ans;
}
private:
multiset<int> heights;
int MaxHeight () {
if (heights.empty())
return 0;
else
return *heights.rbegin();
}
};
网友评论