[LeetCode] The Skyline Problem 天际线问题

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.

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

http://www.shuatiblog.com/blog/2014/07/01/The-Skyline-Problem/

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

http://www.meetqun.com/thread-9571-1-1.html

时间: 2024-10-13 10:40:49

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

[LeetCode] The Skyline Problem

An interesting problem! But not very easy at first glance. You need to think very clear about how will a keypoint be generated. Since I only learn from others' solutions and am still unable to give my personal explanations, I would suggest you to the

解题报告:LeetCode The Skyline Problem(画天际

题目出处:https://leetcode.com/submissions/detail/47013144/题意描述: 给定一系列矩形的左边坐标Li,右边坐标Ri,和高度Hi(其中Li按照从小到大的顺序排列).代表城市中一座座高楼.求这些矩形代表的高楼行成的天际线.天际线的定义为:在远处看这些所有的高楼时看到的轮廓. 数据输入: [ [L1, R1, H1], [Li, Ri, Hi]...]为元素类型为三维向量的向量,其中Li,Ri,Hi的含义见题意描述.且输入数据保证: 0 ≤ Li, Ri

[LeetCode-JAVA] The Skyline Problem

题目:题目太长了,见链接-- > The Skyline Problem 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

解题报告:The Skyline Problem(画天际线)

题目出处:https://leetcode.com/problems/the-skyline-problem/ 题目描述: 输入三元组[Li, Ri, Hi],代表建筑的左右坐标,以及高度,构成图A. 要求画出天际线,如B图所示,输出为[[x1,y1], [x2, y2], [x3, y3], ... ],一串二元组的集合,分别为轮廓的左端点,x代表横坐标,y代表纵坐标高度. 解题思路: 思路一: 首先,看到这道题,最最简单的思路是直接遍历每个x值,简单粗暴正面刚,将每个覆盖x值的区间合并并求出

[LeetCode#218] The Skyline Problem

Problem: 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), w

LeetCode题218——The Skyline Problem

https://leetcode.com/problems/the-skyline-problem/description/ 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

Java for LeetCode 218 The Skyline Problem【Comming Soon】

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 pr

The Skyline Problem leetcode 详解

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({

Leetcode-The Skyline Problem题解

一. 题目 Leetcode平台上天际线问题 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