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. Codeforces C. Elections(贪心枚举三分)

    题目描述: C. Elections time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  2. 2019年牛客多校第一场 I题Points Division 线段树+DP

    题目链接 传送门 题意 给你\(n\)个点,每个点的坐标为\((x_i,y_i)\),有两个权值\(a_i,b_i\). 现在要你将它分成\(\mathbb{A},\mathbb{B}\)两部分,使得 ...

  3. ElementUI——报错汇总

    前言 elementUI的报错汇总 错误 please transfer a valid prop path to form item! vue.esm.js?c5de:628 [Vue warn]: ...

  4. Linux——查询服务器公网IP

    前言 服务器查看IP,十分简单,但是如何查看公网IP呢? 步骤 网站:http://www.cip.cc/ 命令行查询(详细): UNIX/Linux: #curl cip.cc Windows: & ...

  5. async-validator:Element表单验证

    转载文章:Element表单验证(2)   Element表单验证(2) 上篇讲的是async-validator的基本要素,那么,如何使用到Element中以及怎样优雅地使用,就在本篇. 上篇讲到a ...

  6. MyBatis框架的基本要素-核心接口和类的作用范围

    通过上面运行案例-查询用户表中的记录数. 非集成环境下的最佳作用域范围: SqlSessionFactoryBuilder 用过即丢,推荐作用域在方法体内. SqlSessionFactory 最佳作 ...

  7. ActiveMQ基础

    消息队列的作用 为什么使用ActiveMQ,不使用其他工具 下载安装包并启动 http://localhost:8161/admin/ (账号:admin:admin) Java实现步骤: // 1. ...

  8. 阿里巴巴编程规约--digest

    所谓卫语句,如果某个条件极其罕见,就应该单独检查该条件,并在该条件为真时立刻从函数中返回.这样的单独检查常常被称为“卫语句”. 微服务之间将DTO,Req放到一个单独的项目中,相关的项目都依赖这个底层 ...

  9. LeetCode 1000. Minimum Cost to Merge Stones

    原题链接在这里:https://leetcode.com/problems/minimum-cost-to-merge-stones/ 题目: There are N piles of stones ...

  10. php解决大文件断点续传

    核心原理: 该项目核心就是文件分块上传.前后端要高度配合,需要双方约定好一些数据,才能完成大文件分块,我们在项目中要重点解决的以下问题. * 如何分片: * 如何合成一个文件: * 中断了从哪个分片开 ...