LeetCode 218. 天际线问题 (扫描线+优先队列)
先把所有横坐标排序,然后把建筑按横坐标排序。设定每个建筑都包含左不包含有 [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. 天际线问题 (扫描线+优先队列)的更多相关文章
- Java实现 LeetCode 218 天际线问题
218. 天际线问题 城市的天际线是从远处观看该城市中所有建筑物形成的轮廓的外部轮廓.现在,假设您获得了城市风光照片(图A)上显示的所有建筑物的位置和高度,请编写一个程序以输出由这些建筑物形成的天际线 ...
- Leetcode 218.天际线问题
天际线问题 城市的天际线是从远处观看该城市中所有建筑物形成的轮廓的外部轮廓.现在,假设您获得了城市风光照片(图A)上显示的所有建筑物的位置和高度,请编写一个程序以输出由这些建筑物形成的天际线(图B). ...
- 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 ...
- [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 ...
- 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 ...
- [LeetCode#218] The Skyline Problem
Problem: A city's skyline is the outer contour of the silhouette formed by all the buildings in that ...
- Swift LeetCode 目录 | Catalog
请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift 说明:题目中含有$符号则为付费题目. 如 ...
- C#LeetCode刷题-分治算法
分治算法篇 # 题名 刷题 通过率 难度 4 两个排序数组的中位数 C#LeetCode刷题之#4-两个排序数组的中位数(Median of Two Sorted Arrays)-该题未达最优解 30 ...
- leetcode难题
4 寻找两个有序数组的中位数 35.9% 困难 10 正则表达式匹配 24.6% 困难 23 合并K个排序链表 47.4% 困难 25 K ...
- C#LeetCode刷题-线段树
线段树篇 # 题名 刷题 通过率 难度 218 天际线问题 32.7% 困难 307 区域和检索 - 数组可修改 42.3% 中等 315 计算右侧小于当前元素的个数 31.9% 困难 4 ...
随机推荐
- nacos:关于注册服务与配置管理
为什么要用nacos做配置中心? 1.nacos可以做到统一管理,而且在修改时可以做到动态管理,无需重启即可生效. 2.nacos通过namespace进行环境隔离, 约定: namespace:用于 ...
- Apachepoi读写Excel实例
/* * 通过poi创建Excel并写入内容 * */ public static void write() throws IOException { //在内存中创建excel XSSFWorkbo ...
- 如何在mysql中删除重复数据
#分组去重法 讲重复的列进行分组 之后用min(id) #取其中最小的保留,其余的删除 -- 步骤 1: 创建临时表,保存每组最小的ID CREATE TEMPORARY TABLE tmp_keep ...
- 【SVN】提交失败报错
SVN提交失败: 最后信息是提示 请输入日志消息,至少需要20个字符,提交终止 问题原因是: 提交的时候不要把提交信息换行来写,SVN只会读取第一行内容 如果消息没有问题还提交失败,可能是文件因为提交 ...
- 从.net开发做到云原生运维(六)——分布式应用运行时Dapr
1. 前言 上一篇文章我们讲了K8s的一些概念,K8s真的是带来了很多新玩法,就像我们今天这篇文章的主角Dapr一样,Dapr也能在K8s里以云原生的方式运行.当然它也可以和容器一起运行,或者是CLI ...
- 神州笔记本 —— HASEE神州 —— 用户手册(使用功能键)—— 笔记本电脑功能键
功能键功能: FN+f1 启动/关闭 触摸板 FN+f2 启动/关闭 屏幕背光 FN+f3 启动/关闭 喇叭和外接耳机 FN+f5 减低音量 FN+f6 提高音量 FN+f7 切换屏幕 FN+f8 降 ...
- vue3:setup语法糖使用教程
setup语法糖简介 直接在script标签中添加setup属性就可以直接使用setup语法糖了. 使用setup语法糖后,不用写setup函数:组件只需要引入不需要注册:属性和方法也不需要再返回,可 ...
- StartImage.DLL使用说明
StartImage.DLL使用说明 一.库的引入 库包含以下物件,请按照要求将以下库映入到项目中 StartImage.dll StartImage.lib StartImage.h 二.注意事项 ...
- Typora中的markdown语法的学习
markdown语法学习 二级标题 三级标题 四级标题 字体 hello world hello world hello world hello world 引用 我是最nb的 分割线 图片 ctrl ...
- 《黑神话:悟空》神话再现,虚幻引擎与Unity/C#谁更强?
前言 在国产游戏领域,<黑神话:悟空>无疑是一颗耀眼的明星,以独特的艺术风格.深厚的文化底蕴以及卓越的技术表现,赢得了国内外玩家的广泛关注.然而,在这款游戏光鲜亮丽的背后,是我们开发者对技 ...