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. 学习app开发思路

    1.首先在学习之前进行一次或者是整体或者是部分的检测,当第一次检测就通过,则可以认为是熟练掌握的东西(可以在后期对其进行验证是否是熟练)2.后面的学习过程,对回答的正确与否以及从第一次开始学习到目前为 ...

  2. linux的后台运行相关命令

    screen -S name 创建一个名为name的后台,或者说bash面板,在这上面运行的任务不会因为连接断开而退出,且保留bash上的信息 screen -ls 列出所有的screen scree ...

  3. Bootstrap框架简介

    Bootstrap是Twitter公司(www. twitter.com)开发的一个基于HTML , CSS , JavaScript的技术框架,符合HTML和 CSS规范,且代码简洁.视觉优美.该框 ...

  4. Linux 安装Anaconda 提示“bunzip2: command not found”

    问题: 安装Anaconda 过程中提示缺少“bunzip2” 解决思路: 由于缺少bunzip2 包,需要通过yum 方式安装bzip2 yum install -y bzip2 Linux bun ...

  5. C++异常处理(二)----声明接口

    接口声明的三种形式 抛出一切形式的异常 void freeobj(mycoach &t) { ) { cout <<"精神可嘉~但还是年龄太小" << ...

  6. Mixed Far-Field and Near-Field Source Localization Based on Subarray Cross-Cumulant

    基于子阵列互累积量(Cross-Cumulant)的远场和近场混合声源定位[1]. 文中采用Uniform linear array (ULA)阵列,将其分为两个互相重叠的子阵列,构建关于子阵列输出信 ...

  7. 利用window.open如何绕过浏览器拦截机制

    在浏览器的安全机制里,非用户触发的window.open方法是会被拦截的,例如: var btn = $('#btn'); btn.click(function () { // 算做用户触发,所以不会 ...

  8. web安全总结

    一.XSS 首先说下最常见的 XSS 漏洞,XSS (Cross Site Script),跨站脚本攻击,因为缩写和 CSS (Cascading Style Sheets) 重叠,所以只能叫 XSS ...

  9. Cocos CreatorUI系统下

    若本号内容有做得不到位的地方(比如:涉及版权或其他问题),请及时联系我们进行整改即可,会在第一时间进行处理. 请点赞!因为你们的赞同/鼓励是我写作的最大动力! 欢迎关注达叔小生的简书! 这是一个有质量 ...

  10. 第12组 Alpha冲刺(4/6)

    Header 队名:To Be Done 组长博客 作业博客 团队项目进行情况 燃尽图(组内共享) 由于这两天在修bug,燃尽图没有下降 展示Git当日代码/文档签入记录(组内共享) 注: 由于Git ...