LeetCode 218. 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).
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], ...]
要点分解
一开始我以为很简单,但其实这个题目要考虑建筑物之间的重叠(以及一些边界情况的处理),需要用到max priority queue,顾名思义,可以直接取出整个集合中的最大值。
这个max priority queue,Java
实现用的数据结构是TreeMap
,对应到C++
就是map
。
代码(C++)
typedef struct BuildingPoint {
int x;
int height;
bool isStart;
} BuildingPoint;
bool compare(BuildingPoint a, BuildingPoint b) {
if (a.x != b.x) {
return (b.x - a.x) > 0;
} else {
return ((a.isStart ? a.height : -a.height) - (b.isStart ? b.height : -b.height)) > 0;
}
}
class Solution {
public:
vector<pair<int, int>> getSkyline(vector<vector<int>>& buildings) {
std::vector<BuildingPoint> buildingPoints;
int numBuildings = buildings.size();
int index = 0;
for (int i = 0; i < numBuildings; ++i) {
buildingPoints.push_back(BuildingPoint());
buildingPoints[index].x = buildings[i][0];
buildingPoints[index].height = buildings[i][2];
buildingPoints[index].isStart = true;
buildingPoints.push_back(BuildingPoint());
buildingPoints[index + 1].x = buildings[i][1];
buildingPoints[index + 1].height = buildings[i][2];
buildingPoints[index + 1].isStart = false;
index += 2;
}
std::sort(buildingPoints.begin(), buildingPoints.end(), compare);
// start the real processing on each building point
std::map<int, int> maxPriorityQueue;
maxPriorityQueue[0] = 1;
std::map<int, int>::iterator queueIter;
std::vector<std::pair<int, int> > result;
int prevMaxHeight = 0;
// to maintain the max priority queue
for (int i = 0; i < 2*numBuildings; ++i) {
queueIter = maxPriorityQueue.find(buildingPoints[i].height);
if (buildingPoints[i].isStart) {
if (queueIter == maxPriorityQueue.end()) // if this height is not found, add it
maxPriorityQueue.insert(std::pair<int, int>(buildingPoints[i].height, 1));
else
queueIter->second += 1;
} else {
if (queueIter != maxPriorityQueue.end()) {
if (queueIter->second == 1) {
maxPriorityQueue.erase(queueIter);
}
else {
queueIter->second -= 1;
}
}
}
// maintain the max height value
queueIter = maxPriorityQueue.end();
int currentMaxHeight = (--queueIter)->first;
// maintain the result variable
if (currentMaxHeight != prevMaxHeight) {
std::pair<int, int> _res(buildingPoints[i].x, currentMaxHeight);
result.push_back(_res);
prevMaxHeight = currentMaxHeight;
}
}
return result;
}
};