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_MAX, 0 < 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], ...]
class Solution {

public:

    vector<pair<int, int>> getSkyline(vector<vector<int>>& buildings) {

        vector< pair<int, int> > edges;

        //put all of edge into a vector

        //set left edge as negtive, right edge as positive

        //so, when we sort the edges, 

        //  1) for same left point, the height would be descending order

        //  2) for same right point, the height would be ascending order

        int left, right, height;

        for(int i=; i<buildings.size(); i++) {

            left   = buildings[i][];

            right  = buildings[i][];

            height = buildings[i][];

            edges.push_back(make_pair(left, -height));

            edges.push_back(make_pair(right, height));

        }

        sort(edges.begin(), edges.end());

        // 1) if we meet a left edge, then we add its height into a `set`.

        //    the `set` whould sort the height automatically.

        // 2) if we meet a right edge, then we remove its height from the `set`

        //

        // So, we could get the current highest height from the `set`, if the 

        // current height is different with preivous height, then we need add

        // it into the result.

        vector< pair<int, int> > result;

        multiset<int> m;

        m.insert();

        int pre = , cur = ;

        for (int i=; i<edges.size(); i++){

            pair<int,int> &e = edges[i];

            if (e.second < ) {

                m.insert(-e.second);

            }else{

                m.erase(m.find(e.second));

            }

            cur = *m.rbegin();

            if (cur != pre) {

                result.push_back(make_pair(e.first, cur));

                pre = cur;

            }

        }

        return result;

    }

};

/*

* Sweep line with max-heap

* ------------------------

* Notice that "key points" are either the left or right edges of the buildings.

*

* Therefore, we first obtain both the edges of all the N buildings, and store the 2N edges in a sorted array.

* Maintain a max-heap of building heights while scanning through the edge array:

* 1) If the current edge is a left edge, then add the height of its associated building to the max-heap;

* 2) If the edge is a right one, remove the associated height from the heap.

*

* Then we take the top value of the heap (yi) as the maximum height at the current edge position (xi).

* Now (xi, yi) is a potential key point.

*

* If yi is the same as the height of the last key point in the result list, it means that this key point

* is not a REAL key point, but rather a horizontal continuation of the last point, so it should be discarded;

*

* otherwise, we add (xi,yi) to the result list because it is a real key point.

*

* Repeat this process until all the edges are checked.

*

* It takes O(NlogN) time to sort the edge array. For each of the 2N edges,

* it takes O(1) time to query the maximum height but O(logN) time to add

* or remove elements. Overall, this solution takes O(NlogN) time.

*/

218. The Skyline Problem *HARD* -- 矩形重叠的更多相关文章

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

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

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

  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. 218. The Skyline Problem

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

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

  6. 218. The Skyline Problem (LeetCode)

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

  7. [LeetCode] Rectangle Overlap 矩形重叠

    A rectangle is represented as a list [x1, y1, x2, y2], where (x1, y1) are the coordinates of its bot ...

  8. [Swift]LeetCode836. 矩形重叠 | Rectangle Overlap

    A rectangle is represented as a list [x1, y1, x2, y2], where (x1, y1) are the coordinates of its bot ...

  9. LeetCode 836. 矩形重叠

    题目链接:https://leetcode-cn.com/problems/rectangle-overlap/ 矩形以列表 [x1, y1, x2, y2] 的形式表示,其中 (x1, y1) 为左 ...

随机推荐

  1. PCL点云库:对点云进行变换(Using a matrix to transform a point cloud)

    点云数据可以用ASCII码的形式存储在PCD文件中(关于该格式的描述可以参考链接:The PCD (Point Cloud Data) file format).为了生成三维点云数据,在excel中用 ...

  2. sql语句查询出表里符合条件的第二条记录的方法

    创建用到的表的SQL CREATE TABLE [dbo].[emp_pay]( [employeeID] [int] NOT NULL, [base_pay] [money] NOT NULL, [ ...

  3. [SAP ABAP开发技术总结]增强Enhancement

    声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...

  4. [SAP ABAP开发技术总结]局部变量、全局变量

    声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...

  5. iOS - Swift NSTimer 定时器

    前言 public class NSTimer : NSObject 作用 在指定的时间执行指定的任务. 每隔一段时间执行指定的任务. 1.定时器的创建 当定时器创建完(不用 scheduled 的, ...

  6. Redis实践操作之—— keyspace notification(键空间通知)

    一.需求分析: 设置了生存时间的Key,在过期时能不能有所提示? 如果能对过期Key有个监听,如何对过期Key进行一个回调处理? 如何使用 Redis 来实现定时任务? 二.序言: 本文所说的定时任务 ...

  7. htm Dom对象与 Xml Dom对象的理解

    html 是基于Xml的文档规范.是一种特殊的xml文档,这一点很重要 1.xml 文档的操作,java,c#,...各种语言都提供了很好的api对文档进行解析,操作.当然js 也不例外,提供了一系列 ...

  8. js模块化编程总结

    大家都知道,js中的变量(variable)有其作用范围,比如:函数里用var定义的变量在函数外是看不到的,而定义在函数外面的变量(不能有没有var修饰)均是全局变量,在js程序的任何位置都可以访问. ...

  9. poj2451Uyuw's Concert(半平面交)

    链接 逆时针给出线段,如果模板是顺时针的修改下系数的符号进行平面交即可. #include <iostream> #include<cstdio> #include<cs ...

  10. C#中“==”和equals()的区别

    如以下代码: 1 2 3 4 5 6 7 8 9 int age = 25;   short newAge = 25;   Console.WriteLine(age == newAge);  //t ...