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_MAX0 < 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.

这道题一打开又是图又是这么长的题目的,看起来感觉应该是一道相当复杂的题,但是做完之后发现也就那么回事,虽然我不会做,是学习的别人的解法。这道求天际线的题目应该算是比较新颖的题,要是非要在之前的题目中找一道类似的题,也就只有 Merge Intervals了吧,但是与那题不同的是,这道题不是求被合并成的空间,而是求轮廓线的一些关键的转折点,这就比较复杂了,通过仔细观察题目中给的那个例子可以发现,要求的红点都跟每个小区间的左右区间点有密切的关系,而且进一步发现除了每一个封闭区间的最右边的结束点是楼的右边界点,其余的都是左边界点,而且每个红点的纵坐标都是当前重合处的最高楼的高度,但是在右边界的那个楼的就不算了。在网上搜了很多帖子,发现网友Brian Gordon的帖子图文并茂,什么动画渐变啊,横向扫描啊,简直叼到没朋友啊,但是叼到极致后就懒的一句一句的去读了,这里博主还是讲解另一位网友百草园的博客吧。这里用到了 multiset 数据结构,其好处在于其中的元素是按堆排好序的,插入新元素进去还是有序的,而且执行删除元素也可方便的将元素删掉。这里为了区分左右边界,将左边界的高度存为负数,建立左边界和负高度的 pair,再建立右边界和高度的 pair,存入数组中,都存进去了以后,给数组按照左边界排序,这样就可以按顺序来处理那些关键的节点了。在 multiset 中放入一个0,这样在某个没有和其他建筑重叠的右边界上,就可以将封闭点存入结果 res 中。下面按顺序遍历这些关键节点,如果遇到高度为负值的 pair,说明是左边界,那么将正高度加入 multiset 中,然后取出此时集合中最高的高度,即最后一个数字,然后看是否跟 pre 相同,这里的 pre 是上一个状态的高度,初始化为0,所以第一个左边界的高度绝对不为0,所以肯定会存入结果 res 中。接下来如果碰到了一个更高的楼的左边界的话,新高度存入 multiset 的话会排在最后面,那么此时 cur 取来也跟 pre 不同,可以将新的左边界点加入结果 res。第三个点遇到绿色建筑的左边界点时,由于其高度低于红色的楼,所以 cur 取出来还是红色楼的高度,跟 pre 相同,直接跳过。下面遇到红色楼的右边界,此时首先将红色楼的高度从 multiset 中删除,那么此时 cur 取出的绿色楼的高度就是最高啦,跟 pre 不同,则可以将红楼的右边界横坐标和绿楼的高度组成 pair 加到结果 res 中,这样就成功的找到我们需要的拐点啦,后面都是这样类似的情况。当某个右边界点没有跟任何楼重叠的话,删掉当前的高度,那么 multiset 中就只剩0了,所以跟当前的右边界横坐标组成pair就是封闭点啦,具体实现参看代码如下:

class Solution {
public:
vector<pair<int, int>> getSkyline(vector<vector<int>>& buildings) {
vector<pair<int, int>> h, res;
multiset<int> m;
int pre = , cur = ;
for (auto &a : buildings) {
h.push_back({a[], -a[]});
h.push_back({a[], a[]});
}
sort(h.begin(), h.end());
m.insert();
for (auto &a : h) {
if (a.second < ) m.insert(-a.second);
else m.erase(m.find(a.second));
cur = *m.rbegin();
if (cur != pre) {
res.push_back({a.first, cur});
pre = cur;
}
}
return res;
}
};

Github 同步地址:

https://github.com/grandyang/leetcode/issues/281

类似题目:

Falling Squares

Rectangle Area II

参考资料:

https://leetcode.com/problems/the-skyline-problem/

http://www.cnblogs.com/easonliu/p/4531020.html

https://briangordon.github.io/2014/08/the-skyline-problem.html

https://leetcode.com/problems/the-skyline-problem/discuss/61193/Short-Java-solution

LeetCode All in One 题目讲解汇总(持续更新中...)

[LeetCode] 281. The Skyline Problem 天际线问题的更多相关文章

  1. [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 ...

  2. LeetCode 218. The Skyline Problem 天际线问题(C++/Java)

    题目: A city's skyline is the outer contour of the silhouette formed by all the buildings in that city ...

  3. [LeetCode] The Skyline Problem 天际线问题

    A city's skyline is the outer contour of the silhouette formed by all the buildings in that city whe ...

  4. [LeetCode#218] The Skyline Problem

    Problem: A city's skyline is the outer contour of the silhouette formed by all the buildings in that ...

  5. 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 ...

  6. [LeetCode] The Skyline Problem

    A city's skyline is the outer contour of the silhouette formed by all the buildings in that city whe ...

  7. [Swift]LeetCode218. 天际线问题 | The Skyline Problem

    A city's skyline is the outer contour of the silhouette formed by all the buildings in that city whe ...

  8. Leetcode 807 Max Increase to Keep City Skyline 不变天际线

    Max Increase to Keep City Skyline In a 2 dimensional array grid, each value grid[i][j] represents th ...

  9. 218. The Skyline Problem (LeetCode)

    天际线问题,参考自: 百草园 天际线为当前线段的最高高度,所以用最大堆处理,当遍历到线段右端点时需要删除该线段的高度,priority_queue不提供删除的操作,要用unordered_map来标记 ...

随机推荐

  1. Kubernetes Deployment(部署无状态应用)

    Kubernetes Deployment(部署无状态应用) Pod与controllers的关系 • controllers:在集群上管理和运行容器的对象 • 通过label-selector相关联 ...

  2. 基于Spark的电影推荐系统(推荐系统~1)

    第四部分-推荐系统-项目介绍 行业背景: 快速:Apache Spark以内存计算为核心 通用 :一站式解决各个问题,ADHOC SQL查询,流计算,数据挖掘,图计算 完整的生态圈 只要掌握Spark ...

  3. Linux PHP安装xdebug扩展及PHPstorm调试

    前言:使用IDE编辑器的时候如PHPstorm,为了方便调试,这里安装PHP的扩展xdebug.安装环境为Linux centos7.3 一.下载xdebug扩展 官网:https://xdebug. ...

  4. C#面试基础知识点:值类型和引用类型(1)(填坑文)

    目录 前言 C#值类型和引用类型 基类(共同点) 值类型继承基类(不同点) 应用类型继承 技术经理的问题 值类型与引用类型都可以用Equals来比较吗? 如何将一个数组a的值赋予数组b然后对b做修改而 ...

  5. .NET使用本地Outlook邮箱指定邮箱用户名和密码发送邮件

    1.添加Microsoft.Office.Interop.Outlook引用 2.封装发送邮件方法 using System; using System.Configuration; using Sy ...

  6. Unable to connect to web server 'IIS Express'(无法连接到Web服务器“IIS Express”)的解决方式-Jexus Manager

    在运行微软示例工程eShopOnWeb时候, 在经过一段时间再运行启动报Error "Unable to connect to web server 'IIS Express'"  ...

  7. docfx 简单使用方法、自动生成目录的工具

    [摘要] 这是我编写的一个 Docfx 文档自动生成工具,只要写好 Markdown 文档,使用此工具可为目录.文件快速生成配置,然后直接使用 docfx 运行即可. https://github.c ...

  8. python3 用户名和密码三次错误

    一.需求 1)密码输错超过三次进行锁定: 2)如果用户名在锁定文件中提示错误: 二.流程图 三.代码 # Aduthor:CCIP-Ma import sys f=open("passwor ...

  9. JDK1.8新特性——Stream API

    JDK1.8新特性——Stream API 摘要:本文主要学习了JDK1.8的新特性中有关Stream API的使用. 部分内容来自以下博客: https://blog.csdn.net/icarus ...

  10. wpf 工程生成dll

    在WPF项目里,当工程里包含窗体时候, 不可以使用类库的方式生产dll,虽然系统支持引用exe 文件,但总是觉得不如dll习惯,后来发现,新建个项目,类型选择“WPF自定义类件库”,名称和工程名称相同 ...