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 ...
随机推荐
- 【数学建模导论】Task01 数据处理与拟合模型
0 前言 感谢 DataWhale 的开源学习课程 intro-mathmodel,项目仓库在这. 现在开始,跟着Task01 进入数据类的学习,实现每一个代码,包括数据预处理.回归分析与分类分析.假 ...
- Umov移动方块-scratch编程作品
程序说明: <Umov移动方块>是一款基于Scratch平台制作的小游戏.在这个游戏中,玩家将面对一个3×3的圆圈棋盘,并通过鼠标控制蓝色方块在这些圆圈中灵活移动.游戏的挑战在于,舞台的四 ...
- 【Shiro】05 自定义Realm认证实现
[前提情要] Shiro默认使用自带的IniRealm,IniRealm从ini配置文件中读取用户的信息, 大部分情况下需要从系统的数据库中读取用户信息,所以需要自定义realm. 根接口:Realm ...
- 如何拉取指定CPU架构并且指定ubuntu版本并且指定cuda和cudnn版本的docker镜像
本篇讲的重点是如何拉取带有cuda和cudnn的docker镜像,因此这些的镜像源的频道为NVIDIA: 官方地址: https://hub.docker.com/r/nvidia/cuda 根据官方 ...
- mpi4py和cupy的联合应用(anaconda环境):GPU-aware MPI + Python GPU arrays
Demo代码: from mpi4py import MPI import cupy as cp comm = MPI.COMM_WORLD size = comm.Get_size() rank = ...
- 【转载】 Nature再发DeepMind研究:AI复现大脑网格细胞模拟导航!
原文地址: https://news.eeany.cn/news/416675-19.html ==================================================== ...
- MySQL 是如何实现数据的排序的?
1.背景 或许你面试的时候被问到了mysql的排序问题 又或许你在学习排序算法的时候想到了数据库的排序是如何实现的呢 下面重点从面试的角度来回答这个问题 2.面试回答 1.普通面试者回答 普通面试者的 ...
- 学习使用docker-compose搭建Redis哨兵集群
搭建的记录 Redis的容器部署后,redis-server的默认安装目录为 /usr/local/bin. docker volumes 映射的文件与本地的文件共享,修改本地文件后,需要重启dock ...
- python增删查改实例
本文介绍一个实例,即删除数据库中原有的表格TEST1,新建一个表格TEST2,并在TEST2中插入3行数据.插入数据以后,查询出ID=3的数据,读出,最后将其删除. 结果: 代码: ''' impor ...
- blender-主要操作技巧
E 或 G 或 S 之后 不在同一平面的 处理1切换正视图 alt + z 切换透视模式 s + z +0 变成同一平面