扫描线+优先队列 https://leetcode-cn.com/problems/the-skyline-problem/solution/tian-ji-xian-wen-ti-by-leetcode-solution-ozse/

先把所有横坐标排序,然后把建筑按横坐标排序。设定每个建筑都包含左不包含有  [left,right) 这样。然后对于每一个横坐标都先在优先队列压入包含它的建筑

然后再从最高的点开始找,如何不包含该横坐标就弹出。然后剩下的建筑中,都是包含该横坐标的,找最高的那个就是关键点。

要学习一下优先队列自定义排序的写法。

class Solution {
public:
vector<vector<int>> getSkyline(vector<vector<int>>& buildings) {
// 优先队列保存当前最大高度
// 优先队列 按高度排序
auto cmp = [](const pair<int, int>& a, const pair<int, int>& b) -> bool { return a.second < b.second; };
priority_queue<pair<int, int>, vector<pair<int, int>>, decltype(cmp)> q(cmp); vector<int> boundaries; // 保存所有横坐标
for (auto &b: buildings) {
boundaries.push_back(b[0]);
boundaries.push_back(b[1]);
}
sort(boundaries.begin(), boundaries.end()); // 横坐标从小到达排序 // buildings 按 lefti 非递减排序 不需要再次排序
vector<vector<int>> ret;
int n = buildings.size(), idx = 0;
for (auto & b: boundaries) {
while (idx < n && buildings[idx][0] <= b) {
q.emplace(buildings[idx][1], buildings[idx][2]);
idx++;
}
while (!q.empty() && q.top().first <= b) {
q.pop();
}
int maxh = q.empty() ? 0 : q.top().second;
if (ret.empty() || maxh != ret.back()[1]) {
ret.push_back({b, maxh});
}
} return ret;
}
};

LeetCode 218. 天际线问题 (扫描线+优先队列)的更多相关文章

  1. Java实现 LeetCode 218 天际线问题

    218. 天际线问题 城市的天际线是从远处观看该城市中所有建筑物形成的轮廓的外部轮廓.现在,假设您获得了城市风光照片(图A)上显示的所有建筑物的位置和高度,请编写一个程序以输出由这些建筑物形成的天际线 ...

  2. Leetcode 218.天际线问题

    天际线问题 城市的天际线是从远处观看该城市中所有建筑物形成的轮廓的外部轮廓.现在,假设您获得了城市风光照片(图A)上显示的所有建筑物的位置和高度,请编写一个程序以输出由这些建筑物形成的天际线(图B). ...

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

  4. [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 ...

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

  6. [LeetCode#218] The Skyline Problem

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

  7. Swift LeetCode 目录 | Catalog

    请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift    说明:题目中含有$符号则为付费题目. 如 ...

  8. C#LeetCode刷题-分治算法

    分治算法篇 # 题名 刷题 通过率 难度 4 两个排序数组的中位数 C#LeetCode刷题之#4-两个排序数组的中位数(Median of Two Sorted Arrays)-该题未达最优解 30 ...

  9. leetcode难题

    4 寻找两个有序数组的中位数       35.9% 困难     10 正则表达式匹配       24.6% 困难     23 合并K个排序链表       47.4% 困难     25 K ...

  10. C#LeetCode刷题-线段树

    线段树篇 # 题名 刷题 通过率 难度 218 天际线问题   32.7% 困难 307 区域和检索 - 数组可修改   42.3% 中等 315 计算右侧小于当前元素的个数   31.9% 困难 4 ...

随机推荐

  1. ffmpeg精简

    自:http://www.chinavideo.org/viewthread.php?tid=5567&extra=page%3D1&page=2 现在更新一下目前遇到的问题: 我想裁 ...

  2. python中的字符串和列表

    name="1" name='1' name="""1""""" name='''1''' #都为正 ...

  3. SpringBoot全局异常处理及自定义异常

    首先定义自定义返回类,如果自己项目中已经有了自定义返回类只需要将后面的代码做相应的修改即可: import io.swagger.annotations.ApiModelProperty; impor ...

  4. 【Vue】Re11 Vue 与 Webpack

    一.案例环境前置准备: 创建一个空目录用于案例演示 mkdir vue-sample 初始化案例和安装webpack cd vue-sample npm install webpack@3.6.0 - ...

  5. 【Vue】02 Component 组件 & Axios

    Vue自定义组件: 不论任何注册组件的方式:template属性有且仅有一个根节点标签 就是说模版属性的标签只能有一个在最外面 <div id="container-element&q ...

  6. 【SpringSecurity】02 权限控制、自定义登陆、记住我

    [资源过滤 权限控制] 就之前的权限问题,例如一个user1登录成功去访问level1的资源当然没有问题 但是页面还呈现了其他权限的资源,比如level2 & level3 既然呈现给了use ...

  7. 搞IT的为什么不建议搞底层(操作系统、编译器、编程语言)——当你搬进你的新家之后,你会在意这个楼是谁打的地基吗?—— 要站在钱流动的地方

    文字表达引自:https://www.youtube.com/watch?v=KITqGv1qYg8 当你搬进你的新家之后,你会在意这个楼是谁打的地基吗?你猜猜那些打地基的工人赚多少钱,卖你沙发电视机 ...

  8. 网友提问:分层A*算法 —— 大规模寻路算法优化

    网友提问: 相关: https://www.cnblogs.com/devilmaycry812839668/p/10525727.html

  9. 光刻机巨头ASML公布了其最新的品牌短片《站在巨人的肩膀上》

    光刻机巨头ASML公布了其最新的品牌短片<站在巨人的肩膀上>: 荷兰光刻机:ASML使用AI工具midjourney和runway制作宣传片 这个时长1分50秒短片的特别地方在于,它是完全 ...

  10. CCF A类会议 —— CVPR 2022 论文审稿模板

    ============================================= Edit ReviewThank you for accepting to serve as a revie ...