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 ...
随机推荐
- MQTT for UWP
老规矩,先简单介绍下MQTT: MQTT(Message Queuing Telemetry Transport,消息队列遥测传输)是IBM开发的一个即时通讯协议,有可能成为物联网的重要组成部分.该协 ...
- 【读书笔记】iOS-写代码注意事项
一,我是尽早和经常编译的强烈支持者.通常,在写完每个方法或有点难度的代码后,都要尝试进行构建.这是一个好习惯,因为如果在上次成功编译以来添加的代码量很小,那么可以非常容易地缩小编译错误范围.这个方法还 ...
- 多种cell混合使用
有时候我们会碰到一个tableView上有多种cell,这个时候就需要定义多种cell,根据条件判断,当满足某个条件的时候选择某个cell 先看plist文件: Person.h #import &l ...
- 【原】训练自己的haar-like特征分类器并识别物体(3)
在前两篇文章中,我介绍了<训练自己的haar-like特征分类器并识别物体>的前三个步骤: 1.准备训练样本图片,包括正例及反例样本 2.生成样本描述文件 3.训练样本 4.目标识别 == ...
- 记OC迁移至swift中笔记20tips
写久了OC后来写swift,总感觉写着是swift的皮毛,但是实际上是OC的核心,这里整理了OC迁移至swift中的一些小细节. 1 在当前类中,实例方法调用属性以及方法都可以将self省略掉,而且是 ...
- iOS网络-01-NSURLRequest与NSURLConnection
NSURLRequest NSURLRequest封装了一次网络请求所需要的数据,主要封装了以下信息: 请求路径(URL) 请求方法(GET或POST) 请求头 请求体 超时参数 NSURLReque ...
- IOS之Foundation--plist简说
将集合元素通过代码写入plist文件中 主要用来一览代码写入plist文件,在以后的工作中,可能会有字典一样的集合元素,需要你转为plist文件,那么你是选择手动输入plist文件中呢,还是通过以下代 ...
- Redis-stat的安装与使用
redis-stat是一个用ruby写成的监控redis的程序,基于info命令获取信息,而不是通过monitor获取信息 一.安装ruby yum install -y ruby ruby-deve ...
- mongo学习笔记(五):分片
分片 人脸: 代表客户端,客户端肯定说,你数据库分片不分片跟我没关系,我叫你干啥就干啥,没什么好商量的. mongos: 首先我们要了解”片键“的概念,也就是说拆分集合的依据是什么?按照 ...
- 第一篇:微信公众平台开发实战Java版之了解微信公众平台基础知识以及资料准备
相信很多人或多或少听说了微信公众平台的火热.但是开发还是有一点门槛,鉴于挺多朋友问我怎么开发,问多了,自己平时也进行以下总结.所以下面给大家分享一下我的经验: 微信公众号是什么? 官网的介绍:再小的个 ...