扫描线+优先队列 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. GUI随笔

    ####GUI是一个很大的话题,从Win32(windows基础API编程)到MFC,QT再到DuiLib,WPF,Winform再到Html这是一个很漫长的路,下面是我对这个界面库的见解 就对我而言 ...

  2. 2023/4/13 SCRUM个人博客

    1.我昨天的任务 ------------ 2.遇到了什么困难 ------------ 3.我今天的任务 初步了解项目的整体框架,并对接下来的人脸识别库以及组件有基本了解和安装

  3. Go 使用 Cobra 构建 CLI 程序

    使用 cobra-cli 搭建手脚架 # 安装 cobra-cli go install github.com/spf13/cobra-cli@latest # 创建一个应用 mkdir myapp ...

  4. BI 工具如何助力市政设计公司实现数字化转型?

    一.前言 近年来,国家出台多个政策文件来鼓励和发展数字化和智能化,如<十四五规划>提出要推进产业数字化转型.<交通强国建设纲要>提出要大力发展智慧交通.上海市发布的<关于 ...

  5. GeoScene Enterprise 3.1 临时许可更新

    Portal许可更新 portal 的许可更新很简单,直接打开Portal在线更新就好了 平台管理 -> 许可管理 -> 附加许可 -> 导入许可 -> 选择文件(选择授权的j ...

  6. javaDoc生成方式

    命令行生成 在cmd控制台窗口上找到需要生成文件的路径,然后执行命令. # javadoc -encoding UTF-8 -charset UTF-8 文件名 javadoc -encoding U ...

  7. 【C】Re06 数组与指针

    一.指针和数组 void pointerAndArray() { int array[5] = {1, 2, 3, 4, 5}; printf("pointer array -> %p ...

  8. 拜登开始在YouTube上打竞选广告了 —— 美国总统的竞选广告已经开始媒体投放了

    哈哈哈,老拜登,跑到YouTube上打广告了,这个画面真的太难想象,如果美国有"椰树"广告,估计拜登能弄个泳装上去打广告.有时不得不佩服西方搞的这种全民选举,最后搞的就和看小品似的 ...

  9. 作为电脑屏幕的补光灯,到底是应该选Led灯还是荧光灯

    现在的台灯灯具市场基本被Led灯给霸占,这就无形之中要大家买台灯的时候只能选择Led等,我也是如此,手上有一款20年前上高中时候的"孩视宝"荧光灯的台灯,然后还有一款刚刚购入的Le ...

  10. Java类和对象 小白版

    一.类 一.类的定义 具有同种属性的对象称为类.定义了它所包含的全体对象的公共特征和功能,对象就是类的一个实例化. 类的三种常见成员:属性.方法.构造器 二.类的编写 1.类名的定义: 2.类属性(特 ...