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).
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], ...]
Credits:
Special thanks to @stellari for adding this problem, creating these two awesome images and all test cases.
使用扫描线法进行处理。左边点标为进入,右边点标为离开,实时维护“活动楼列表”。将同一横坐标的进入点排在前边,离开点排在后边。首先判断x点处的进入点的最高值,并将这些点加入“活动楼列表”,然后判断离开点的最高值,同时将这些点从“活动楼列表”中删除,若最高值等于当前的高度,则输出当前“活动楼列表”的最大高度。
PS:使用set或multiset维护活动楼列表时,当删除某一离开点高度时,会将该高度的所有相同值都删除,造成“活动楼列表“高度为0,产生错误,因此需在输入数据时维护一个高度列表,并在”活动楼列表“中记录进入点的高度在高度列表的坐标,避免同时删除相同高度的点。
class Solution {
private:
#define LEFT 0;
#define RIGHT 1;
struct xEVENT
{
int x;
int height_index;
int side;
xEVENT(int _x,int _height, int _side): x(_x),height_index(_height),side(_side){}
};
private:
static bool compareevent(const xEVENT& e1,const xEVENT& e2)
{
if(e1.x!=e2.x)
return e1.x<e2.x;
return e1.side<e2.side;
}
public:
vector<pair<int, int>> getSkyline(vector<vector<int>>& buildings) {
int n=buildings.size(); vector<pair<int,int>> res;
if(n<)
return res;
vector<xEVENT> event;
vector<int> buildingheight;
set<int> activebuilding;
activebuilding.insert(); for(int i=;i<buildings.size();i++)
{
auto &b=buildings[i];
int index=buildingheight.size();
event.push_back(xEVENT(b[],index,)); event.push_back(xEVENT(b[],index,));
buildingheight.push_back(b[]);
}
sort(event.begin(),event.end(),compareevent);
int curheight=;
pair<int,int> tmp_pair;
for(int i=;i<event.size();i++)
{
if(event[i].side==)
{
activebuilding.insert(event[i].height_index);
int newheight=buildingheight[event[i].height_index];
int newx=event[i].x;
while(i+<event.size()&&event[i+].x==newx&&event[i+].side==)
{
i++;
activebuilding.insert(event[i].height_index);
newheight=max(newheight,buildingheight[event[i].height_index]);
}
if(newheight>curheight)
{
res.push_back(tmp_pair=make_pair(newx,newheight));
curheight=newheight;
}
}
else
{
activebuilding.erase(event[i].height_index);
int newheight=buildingheight[event[i].height_index];
int newx=event[i].x;
while(i+<event.size()&&event[i+].x==event[i].x&&event[i+].side==)
{
i++;
activebuilding.erase(event[i].height_index);
newheight=max(newheight,buildingheight[event[i].height_index]);
}
if(newheight==curheight)
{
int maxheight=;
multiset<int>:: iterator it=activebuilding.begin();
for(;it!=activebuilding.end();it++)
{
maxheight=max(maxheight,buildingheight[*it]);
}
if(maxheight<newheight)
{
res.push_back(tmp_pair=make_pair(newx,maxheight));
curheight=maxheight;
}
}
}
}
return res;
}
};
The Skyline Problem的更多相关文章
- [LeetCode] The Skyline Problem 天际线问题
A city's skyline is the outer contour of the silhouette formed by all the buildings in that city whe ...
- [LeetCode] The Skyline Problem
A city's skyline is the outer contour of the silhouette formed by all the buildings in that city whe ...
- [LeetCode#218] The Skyline Problem
Problem: A city's skyline is the outer contour of the silhouette formed by all the buildings in that ...
- [LeetCode] 281. The Skyline Problem 天际线问题
A city's skyline is the outer contour of the silhouette formed by all the buildings in that city whe ...
- [LeetCode] 218. The Skyline Problem 天际线问题
A city's skyline is the outer contour of the silhouette formed by all the buildings in that city whe ...
- Java for LeetCode 218 The Skyline Problem【HARD】
A city's skyline is the outer contour of the silhouette formed by all the buildings in that city whe ...
- UVa 105 - The Skyline Problem(利用判断,在于想法)
题目来源:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=3&pa ...
- 218. The Skyline Problem *HARD* -- 矩形重叠
A city's skyline is the outer contour of the silhouette formed by all the buildings in that city whe ...
- 218. The Skyline Problem
题目: A city's skyline is the outer contour of the silhouette formed by all the buildings in that city ...
随机推荐
- UINavigationBar 和 UINavigationItem的属性设置
#import "RootViewController.h" @interface RootViewController () @end @implementation RootV ...
- OC数组排序
NSArray *array = @[@"tailong", @"kaersasi", @"airuiliya", @"yingl ...
- 关于报malformed or corrupted AST file: 'Unable to load module 的错~
今天早上 一运行程序 居然报错,我都惊呆了,昨天明明好好的-但是百度是强大的- 报错内容: malformed or corrupted AST file: 'Unable to load modul ...
- iOS开发者必备的10款工具
当前iOS和Android两大移动操作系统“二足鼎立”,几乎覆盖了市面上大部分的智能手机.相比Android,iOS开发适配更简单,且随着各种实用工具和Swift语言的出现,iOS开发门槛地降低,开发 ...
- Linux磁盘、目录、文件操作命令
0x01. Linux磁盘分区与目录结构 ① 主分区.拓展分区.逻辑分区:早期主引导扇区MBR用64B存放主分区信息,每个分区用16B,因而上限为4个主分区,后来,因分区需求,引入拓展分区(类主分区) ...
- 使用MiniProfiler调试ASP.NET MVC网站性能
MiniProfiler 以前开发Webform的时候可以开启trace来跟踪页面事件,这对于诊断程序的性能是有很大的帮助的,起到事半功倍的作用,今天我就来谈用mvc开 发项目的调试和性能监控.EF框 ...
- 机器学习六--K-means聚类算法
机器学习六--K-means聚类算法 想想常见的分类算法有决策树.Logistic回归.SVM.贝叶斯等.分类作为一种监督学习方法,要求必须事先明确知道各个类别的信息,并且断言所有待分类项都有一个类别 ...
- Java学习总结:飘逸的字符串
Java学习:飘逸的字符串 前言 相信不管我们运用Java语言来开发项目还是进行数据分析处理,都要运用到和字符串相关的处理方法.这个社会处处有着和字符串相关的影子:日志.文档.书籍等.既然我们离不开字 ...
- [原]openstack-kilo--issue(五) neutron-agent服务实际是active的-但是显示为XXX
问题出现: 重启后出现了这样的情况: 查看详细的参数 查看数据库neutron 中对应的agents表.发现表中没有alive这个字段 这些服务的实际状态为active: ----1------● n ...
- luluzero的angularJs学习之路_angularJs示例代码
最近开始自学 angularJs这个前端MVC框架,感觉在前端实现MVC很酷有木有.哈哈哈... 先说说我对前端MVC的一个基本的理解吧(刚开始学习接触得还比较浅显,理解可能会有些不到位,还请各位大神 ...