扫描线+优先队列 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. 释放资源的方式try-with-resources

    1.try-catch-finally 2.try-with-resources 使用方法 try(//这里定义你要使用的资源){} catch(){} 注意:try()里只能存放流对象(资源对象), ...

  2. 【Hibernate】Re03 注解方式实现

    使用JPA规范提供的注解即可实现,这样的好处是不需要配置Entity.hbm.xml文件了 但是考虑到多表查询的情况,还是会有xml配置的需要. 一.常用的JPA注解: 1.public @inter ...

  3. 【Spring】07 后续的学习补充 vol1

    控制反转Inverse Of Control的演变: 在之前的原生Javaweb项目的问题: 我们三层架构每一层之间的联系是这样的: 由GradeDao接口指向GradeDaoImpl 再由Grade ...

  4. 实现一个终端文本编辑器来学习golang语言

    欢迎!这个系列的博文会带你使用golang语言来编写一个你自己的文本编辑器. 首先想说说写这个系列文章的动机. 其实作为校招生加入某头部互联网大厂一转眼已经快4年了.可以说该大厂算是比较早的用gola ...

  5. x86_64 ubuntu22.04环境下编译版本python3.13.0 alpha 0源码——python3.13.0 alpha 0的源码编译

    python3.13.0 alpha 0版本源码编译: 环境--x86_64 ubuntu22.04系统: 1. 源码下载: git clone https://github.com/python/c ...

  6. tomcat发布两个项目报错webAppKey重复设置

    两个项目的web.xml中都有一个日志监听器配置 <listener> <listener-class> org.springframework.web.util.Log4jC ...

  7. 如何arm入门

    因为有些需求[原因在文末],需要拍一张正装照. 我已经受够了那些小摄像馆拍的照片了, 完全拍不出我的神韵! 效果还不如我手机开个美颜! 生活问题,有时候问邻居效率很高! 在小区群里,问了一下邻居! 立 ...

  8. 如何将一个模块文件编译到Linux内核中?

    很多粉丝在群里提问,如何把一个模块文件编译到内核中或者独立变异成ko文件.本文给大家详解讲解. 1. 内核目录 Linux内核源代码非常庞大,随着版本的发展不断增加.它使用目录树结构,并且使用Make ...

  9. 流体饱和多孔介质的本构关系 + Föppl-von Kármán 方程

    向有液体的多孔介质上施加应力,应力一部分分布到骨架上,一部分分布到孔隙流体上.骨架上的应力会导致变形,所以被称为 "有效应力".这里考虑拉伸应力为正,有效应力原理写为 \[\sig ...

  10. CF650D Zip-line

    CF650D Zip-line 大概题面: 给定一个长度为 \(n\) 的序列以及\(m\)个操作,每个操作形如" \(a_i,b_i\) ",表示将序列中第 \(a_i\) 个数 ...