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. SQL进阶系列之9用SQL处理数列

    写在前面 关系模型的数据结构里,并没有顺序的概念,但SQL处理有序集合也有坚实的理论基础 生成连续编号 --生成连续编号 CREATE TABLE Digits (digit INTEGER PRIM ...

  2. 关于Java的i++和++i的区别

    之前对于 i++ 和 ++i 的理解就是: int i=1,a=0; 1.i++ 先运算在赋值,例如 a=i++,先运算a=i,后运算i=i+1,所以结果是a==1 2.++i 先赋值在运算,例如 a ...

  3. navicat 远程连接服务器1130,1045问题报错处理

    本人踩坑多次,一开始网上搜罗,解决办法大同小异,摸索了很久才全部解决完成,小小bug真磨人啊 首先,根据我的踩坑记录,navicat 1045和navicat 1130貌似属于同一种解决方案,都是修改 ...

  4. MySQL 日期格式化,取年月日等相关操作

    日期取年.月.日 select id, phone,time,year(time),month(time), DAY(time),TIME(time) from user where phone='x ...

  5. web api 2.0 上传文件超过4M时,出现404错误

    客户端代码 string path = "C:\\text.txt"; WebClient client = new WebClient(); Uri _address = new ...

  6. Scanner的常用用法

    通过new Scanner(System.in)创建一个Scanner,控制台会一直等待输入,直到敲回车键结束,把所输入的内容传给Scanner. s.useDelimiter(" |,|\ ...

  7. webpack的plugin原理

    plugin是webpack生态的重要组成,它为用户提供了一种可以直接访问到webpack编译过程的方式.它可以访问到编译过程触发的所有关键事件. 1. 基本概念 1. 如何实现一个插件 1. plu ...

  8. 创建nextcloud所需的数据库和账户

      创建 nextcloud 所需的数据库和账户 打开数据库管理命令行,默认root没密码,回车进入 sudo mysql -u root -p 创建 nextcloud 数据库,命令包含后面的分号 ...

  9. walk

    一个go做gui的包, 可以配置程序图标 编译运行需要...fest文件 rsrc

  10. 个人Vim配置(即vim目录下vimrc_)

    因为是C++选手所以大部分带有Dev遗留的...格式 colorscheme molokai"配色方案,注意molokai不是自带而是自己调配的,SublimeText3标准配色,想要的点这 ...