扫描线+优先队列 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. 微服务:gateway

    网关路由: 1.创建新模块 2.引入网关依赖 <!--网关--> <dependency> <groupId>org.springframework.cloud&l ...

  2. SpringTask

    SpringTask是spring提供的一个任务调度工具,按照约定的时间自动执行代码逻辑 定时任务框架,即定时自动执行某段代码 应用场景:信用卡每月还款提醒,火车售票系统处理未支付订单 cron表达式 ...

  3. 【XML】Extensible Markup Language 可扩展标记语言

    Extensible Markup Language 可扩展标记语言[XML] 视频资料参考自:https://www.bilibili.com/video/BV1B441117Lu?p=186 其他 ...

  4. 人工智能领域在显卡短缺之下的思考——“【亦】微笑面对显卡短缺:不买更快乐”有感——mindspore快快成长

    划水之时看了看B站视频: [亦]微笑面对显卡短缺:不买更快乐 https://www.bilibili.com/video/BV1RZ4y1c7qT 在显卡短缺之下不仅仅电脑游戏玩家被卡脖子就连高校和 ...

  5. How to 'apt-get install python-opengl' on Ubuntu22.04

    ImportError: Error occurred while running `from pyglet.gl import *` HINT: make sure you have OpenGL ...

  6. 文本相似度 HanPL汉语言处理

    @ 目录 前言 需求 简介 实操开始 1. 添加pom.xml依赖 2. 文本相似度工具类 3. 案例验证 4. 验证结果 总结 前言 请各大网友尊重本人原创知识分享,谨记本人博客:南国以南i. 提示 ...

  7. php 常用文件操作

    判断文件或文件夹是否存在 file_exists() 打开文件 fopen() 关闭文件 fclose() 判断是否可写入 is_writable() 写入数据 fwrite() 测试文件指针是否到了 ...

  8. 增强用户体验:2个功能强大的.NET控制台应用帮助库

    前言 对于.NET开发者而言,构建控制台应用程序时,如何提升用户交互的流畅性和满意度,是一个持续探索与优化的话题.今天大姚给大家分享2个功能强大的.NET控制台应用帮助库,希望可以帮助大家能够快速的构 ...

  9. 通过JUnit源码分析学习编程的奇技淫巧

    打开 Maven仓库,左边选项栏排在第一的就是测试框架与工具,今天的文章,V 哥要来聊一聊程序员必备的测试框架JUnit 的源码实现,整理的学习笔记,分享给大家. 有人说,不就一个测试框架嘛,有必要去 ...

  10. 使用Web Component定义自己的专属网页组件

    什么是Web Component Web Component是一套Web浏览器的技术和规范,能够让开发者定制自己的HTML元素 来自MDN的描述: Web Component 是一套不同的技术,允许你 ...