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.

如果按照矩形来处理将会非常麻烦,可以把这些矩形拆成两个点,一个左上顶点,一个右上顶点,这个题相当于处理2*n个边的问题,每一个边有一个x-axis和一个height,把所给的triplet转换成[x-axis, height],对这些顶点按x-axis排序。然后开始遍历这些点,用一个最大堆来记录高度,对于左顶点,将height加入堆中。对于右顶点,从堆中移出height,同时也意味这这个矩形的结束。堆顶是所有顶点中最高的点,是当前图形的最高位置。只要这个点没被移出堆,说明这个最高的矩形还没结束。如果堆顶高度值出现了变化,说明出现了拐点,记录相应位置到结果中。

具体代码中,为了在排序后的顶点列表中区分左右顶点,同一个图形的左右顶点height一个是正数值,一个是负数值。

(1) 自建一个名为Height的数据结构,保存一个building的index和height。约定,当height为负数时表示这个高度为height的building起始于index;height为正时表示这个高度为height的building终止于index。

(2) 对building数组进行处理,每一行[ Li, Ri, Hi ],根据Height的定义,转换为两个Height的对象,即,Height(Li, -Hi) 和 Height(Ri, Hi)。 将这两个对象存入heights这个List中。

(3) 写个Comparator对heights进行升序排序,首先按照index的大小排序,若index相等,则按height大小排序,以保证一栋建筑物的起始节点一定在终止节点之前。

(4) 将heights转换为结果。使用PriorityQueue对高度值进行暂存。遍历heights,遇到高度为负值的对象时,表示建筑物的起始节点,此时应将这个高度加PriorityQueue。遇到高度为正值的对象时,表示建筑物的终止节点,此时应将这个高度从PriorityQueue中除去。且在遍历的过程中检查,当前的PriorityQueue的peek()是否与上一个iteration的peek()值(prev)相同,若否,说明出现了拐点,则应在结果中加入[当前对象的index, 当前PriorityQueue的peek()],并更新prev的值。

Java:

class Edge {
int x;
int height;
boolean isStart; public Edge(int x, int height, boolean isStart) {
this.x = x;
this.height = height;
this.isStart = isStart;
}
} public List<int[]> getSkyline(int[][] buildings) {
List<int[]> result = new ArrayList<int[]>(); if (buildings == null || buildings.length == 0
|| buildings[0].length == 0) {
return result;
} List<Edge> edges = new ArrayList<Edge>(); // add all left/right edges
for (int[] building : buildings) {
Edge startEdge = new Edge(building[0], building[2], true);
edges.add(startEdge);
Edge endEdge = new Edge(building[1], building[2], false);
edges.add(endEdge);
} // sort edges
Collections.sort(edges, new Comparator<Edge>() {
public int compare(Edge a, Edge b) {
if (a.x != b.x)
return Integer.compare(a.x, b.x); if (a.isStart && b.isStart) {
return Integer.compare(b.height, a.height);
} if (!a.isStart && !b.isStart) {
return Integer.compare(a.height, b.height);
} return a.isStart ? -1 : 1;
}
}); // process edges
PriorityQueue<Integer> heightHeap = new PriorityQueue<Integer>(10, Collections.reverseOrder()); for (Edge edge : edges) {
if (edge.isStart) {
if (heightHeap.isEmpty() || edge.height > heightHeap.peek()) {
result.add(new int[] { edge.x, edge.height });
}
heightHeap.add(edge.height);
} else {
heightHeap.remove(edge.height); if(heightHeap.isEmpty()){
result.add(new int[] {edge.x, 0});
}else if(edge.height > heightHeap.peek()){
result.add(new int[]{edge.x, heightHeap.peek()});
}
}
} return result;
}  

Java:

public class Solution {
public List<int[]> getSkyline(int[][] buildings) {
List<int[]> result = new ArrayList<int[]>();
if (buildings == null || buildings.length == 0 || buildings[0].length == 0) {
return result;
} List<Height> heights = new ArrayList<Height>();
for (int[] building : buildings) {
heights.add(new Height(building[0], -building[2]));
heights.add(new Height(building[1], building[2]));
}
Collections.sort(heights, new Comparator<Height>() {
@Override
public int compare(Height h1, Height h2) {
return h1.index != h2.index ? h1.index - h2.index : h1.height - h2.height;
}
}); PriorityQueue<Integer> pq = new PriorityQueue<Integer>(1000, Collections.reverseOrder());
pq.offer(0);
int prev = 0;
for (Height h : heights) {
if (h.height < 0) {
pq.offer(-h.height);
} else {
pq.remove(h.height);
}
int cur = pq.peek();
if (cur != prev) {
result.add(new int[]{h.index, cur});
prev = cur;
}
} return result;
} class Height {
int index;
int height;
Height(int index, int height) {
this.index = index;
this.height = height;
}
}
}

Python:

class Solution(object):
def getSkyline(self, buildings):
n = len(buildings)
points = sorted([(buildings[i][0], buildings[i][2], 's') for i in range(n)] +
[(buildings[i][1], buildings[i][2], 'e') for i in range(n)])
print points
result, maxHeap = [], []
for p in points:
pre_height = - maxHeap[0] if maxHeap else 0
if p[2] == 's':
heappush(maxHeap, -p[1])
else:
heappop(maxHeap)
cur_height = - maxHeap[0] if maxHeap else 0 if p[2] == 's' and p[1] > pre_height:
result.append([p[0], p[1]])
elif p[2] == 'e' and p[1] > cur_height:
result.append([p[0], cur_height]) return result

Python:

from heapq import heappush, heappop
import functools class Edge(object):
def __init__(self, x, height, is_start):
self.x = x
self.height = height
self.is_start = is_start def __repr__(self):
return repr((self.x, self.height, self.is_start)) class Solution1(object):
def compare(self, edge1, edge2):
if edge1.x != edge2.x:
return -1 if edge1.x < edge2.x else 1 if ((edge1.is_start and edge2.is_start) or
(not edge1.is_start and not edge2.is_start)):
return -1 if edge1.height < edge2.height else 1 return -1 if edge1.is_start else 1 def getSkyline(self, buildings):
edges = []
for building in buildings:
start_edge = Edge(building[0], building[2], True)
end_edge = Edge(building[1], building[2], False)
edges.extend((start_edge, end_edge))
sorted_edges = sorted(edges, key=functools.cmp_to_key(self.compare))
print sorted_edges
maxHeap, result = [], []
for edge in sorted_edges:
if edge.is_start:
if not maxHeap or edge.height > - maxHeap[0]:
result.append((edge.x, edge.height))
heappush(maxHeap, - edge.height)
else:
heappop(maxHeap)
if not maxHeap:
result.append((edge.x, 0))
elif edge.height > - maxHeap[0]:
result.append((edge.x, - maxHeap[0])) return result

Python:

class MaxHeap:
def __init__(self, buildings):
self.buildings = buildings
self.size = 0
self.heap = [None] * (2 * len(buildings) + 1)
self.lineMap = dict()
def maxLine(self):
return self.heap[1]
def insert(self, lineId):
self.size += 1
self.heap[self.size] = lineId
self.lineMap[lineId] = self.size
self.siftUp(self.size)
def delete(self, lineId):
heapIdx = self.lineMap[lineId]
self.heap[heapIdx] = self.heap[self.size]
self.lineMap[self.heap[self.size]] = heapIdx
self.heap[self.size] = None
del self.lineMap[lineId]
self.size -= 1
self.siftDown(heapIdx)
def siftUp(self, idx):
while idx > 1 and self.cmp(idx / 2, idx) < 0:
self.swap(idx / 2, idx)
idx /= 2
def siftDown(self, idx):
while idx * 2 <= self.size:
nidx = idx * 2
if idx * 2 + 1 <= self.size and self.cmp(idx * 2 + 1, idx * 2) > 0:
nidx = idx * 2 + 1
if self.cmp(nidx, idx) > 0:
self.swap(nidx, idx)
idx = nidx
else:
break
def swap(self, a, b):
la, lb = self.heap[a], self.heap[b]
self.lineMap[la], self.lineMap[lb] = self.lineMap[lb], self.lineMap[la]
self.heap[a], self.heap[b] = lb, la
def cmp(self, a, b):
return self.buildings[self.heap[a]][2] - self.buildings[self.heap[b]][2] class Solution:
def getSkyline(self, buildings):
size = len(buildings)
points = sorted([(buildings[x][0], x, 's') for x in range(size)] +
[(buildings[x][1], x, 'e') for x in range(size)])
maxHeap = MaxHeap(buildings)
ans = []
for p in points:
if p[2] == 's':
maxHeap.insert(p[1])
else:
maxHeap.delete(p[1])
maxLine = maxHeap.maxLine()
height = buildings[maxLine][2] if maxLine is not None else 0
if len(ans) == 0 or ans[-1][0] != p[0]:
ans.append([p[0], height])
elif p[2] == 's':
ans[-1][1] = max(ans[-1][1], height)
else:
ans[-1][1] = min(ans[-1][1], height)
if len(ans) > 1 and ans[-1][1] == ans[-2][1]:
ans.pop()
return ans

C++: heap

class Solution {
private:
enum NODE_TYPE {LEFT, RIGHT};
struct node {
int x, y;
NODE_TYPE type;
node(int _x, int _y, NODE_TYPE _type) : x(_x), y(_y), type(_type) {}
}; public:
vector<pair<int, int>> getSkyline(vector<vector<int>>& buildings) {
vector<node> height;
for (auto &b : buildings) {
height.push_back(node(b[0], b[2], LEFT));
height.push_back(node(b[1], b[2], RIGHT));
}
sort(height.begin(), height.end(), [](const node &a, const node &b) {
if (a.x != b.x) return a.x < b.x;
else if (a.type == LEFT && b.type == LEFT) return a.y > b.y;
else if (a.type == RIGHT && b.type == RIGHT) return a.y < b.y;
else return a.type == LEFT;
}); priority_queue<int> heap;
unordered_map<int, int> mp;
heap.push(0);
vector<pair<int, int>> res;
int pre = 0, cur = 0;
for (auto &h : height) {
if (h.type == LEFT) {
heap.push(h.y);
} else {
++mp[h.y];
while (!heap.empty() && mp[heap.top()] > 0) {
--mp[heap.top()];
heap.pop();
}
}
cur = heap.top();
if (cur != pre) {
res.push_back({h.x, cur});
pre = cur;
}
}
return res;
}
};  

C++: Multiset

class Solution {
public:
vector<pair<int, int>> getSkyline(vector<vector<int>>& buildings) {
vector<pair<int, int>> h, res;
multiset<int> m;
int pre = 0, cur = 0;
for (auto &a : buildings) {
h.push_back({a[0], -a[2]});
h.push_back({a[1], a[2]});
}
sort(h.begin(), h.end());
m.insert(0);
for (auto &a : h) {
if (a.second < 0) 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;
}
};

 

类似题目:

[LintCode] 131. Building Outline

All LeetCode Questions List 题目汇总

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

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

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

  3. [LeetCode#218] The Skyline Problem

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

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

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

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

  6. 218. The Skyline Problem (LeetCode)

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

  7. 218. The Skyline Problem

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

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

  9. [LeetCode] The Skyline Problem

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

随机推荐

  1. PAT甲级1010踩坑记录(二分查找)——10测试点未过待更新

    题目分析: 首先这题有很多的坑点,我在写完之后依旧还有第10个测试点没有通过,而且代码写的不优美比较冗长勿喷,本篇博客用于记录写这道题的一些注意点 1.关于两个不同进制的数比大小一般采用将两个数都转化 ...

  2. Python爬取mn52网站美女图片以及图片防盗链的解决方法

    防盗链原理 http标准协议中有专门的字段记录referer 一来可以追溯上一个入站地址是什么 二来对于资源文件,可以跟踪到包含显示他的网页地址是什么 因此所有防盗链方法都是基于这个Referer字段 ...

  3. .Net Core 遇到 “'windows-1252' is not a supported encoding name.”

    使用iTextSharp生成Pdf文件时报错如下: 'windows-1252' is not a supported encoding name. For information on defini ...

  4. DockerToolbox安装docker - Windows 10家庭版

    由于本机使用的是win10家庭版操作系统,无法直接Docker for Windows安装,因此只好使用Docker Toolbox.在此记录一下过程,以供参考. 下载 因为toolbox安装包的官网 ...

  5. junit3和junit4的区别总结

    先来看一个例子: 先用junit3来写测试用例,如下: junit3测试结果: 从上面可看出: 1.junit3必须要继承TestCase类 2.每次执行一个测试用例前,junit3执行一遍setup ...

  6. WinDbg常用命令系列---日志操作相关命令log*

    .logopen (Open Log File) .logopen命令将事件和命令的副本从调试器命令窗口发送到新的日志文件. .logopen [Options] [FileName] .logope ...

  7. nodejs之MongoDB安装[windows平台]

    下载MongoDB,本为下载msi文件安装,下载地址     下载完成之后直接双击文件安装,安装时注意安装路径 创建一个空文件夹,用于存放数据库文件 通过控制台进入安装目录下的bin目录,或者在bin ...

  8. Linux 服务器快速搭建 Java Web 开发环境

    搭建 Java 环境 yum list java* yum install java-1.8.0-openjdk.x86_64 -y java -version mkdir 创建文件夹 mv 修改文件 ...

  9. javascript加密之md5加密

    原作地址:JavaScriptMd5 修改备用:JavaScriptMd5.rar

  10. Android中创建自定义控件

    1.创建一个TitleLayout继承LinearLayout: //创建自定义控件 public class TitleLayout extends LinearLayout { private f ...