leetcode218 天际线问题

来自leetcode题解:扫描线法AlgsCG
class Solution {
public:
vector<vector<int>> getSkyline(vector<vector<int>>& buildings) {
vector<pair<int,int>> h;
multiset<int> m;
vector<vector<int>> res;
//采用从左到右扫描的思想来做;
//1、将每一个建筑分成“两个部分”,例如:[2,9,10]可以转换成[2,-10][9,10],我们用负值来表示左边界
for(const auto& b:buildings)
{
h.push_back({b[0], -b[2]});
h.push_back({b[1], b[2]});
}
//2、根据x值对分段进行排序
sort(h.begin(),h.end());
int prev = 0, cur = 0;
m.insert(0);
//3、遍历
for (auto i:h)
{
if (i.second < 0) m.insert(-i.second); //左端点,高度入堆
else m.erase(m.find(i.second)); //右端点,高度出堆
cur = *m.rbegin(); //还在当前扫描坐标内的当前最大高度
if (cur != prev) { //当前最大高度不等于最大高度perv表示这是一个转折点
res.push_back({i.first, cur}); //添加坐标
prev = cur; //更新最大高度
}
}
return res;
}
};
leetcode218 天际线问题的更多相关文章
- [Swift]LeetCode218. 天际线问题 | The Skyline Problem
A city's skyline is the outer contour of the silhouette formed by all the buildings in that city whe ...
- leetcode 日常清单
a:excellent几乎一次ac或只有点小bug很快解决:半年后再重刷: b:经过艰难的debug和磕磕绊绊或者看了小提示才刷出来: c:经过艰难的debug没做出来,看答案刷的: 艾宾浩斯遗忘曲线 ...
- [LeetCode] The Skyline Problem 天际线问题
A city's skyline is the outer contour of the silhouette formed by all the buildings in that city whe ...
- [Swift]LeetCode807. 保持城市天际线 | Max Increase to Keep City Skyline
In a 2 dimensional array grid, each value grid[i][j]represents the height of a building located ther ...
- Leetcode 807 Max Increase to Keep City Skyline 不变天际线
Max Increase to Keep City Skyline In a 2 dimensional array grid, each value grid[i][j] represents th ...
- [LeetCode] Max Increase to Keep City Skyline 保持城市天际线的最大增高
In a 2 dimensional array grid, each value grid[i][j] represents the height of a building located the ...
- SkylineGlobe 如何二次开发实现天际线分析功能
天际线效果: 示例代码: <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <h ...
- Leetcode 218.天际线问题
天际线问题 城市的天际线是从远处观看该城市中所有建筑物形成的轮廓的外部轮廓.现在,假设您获得了城市风光照片(图A)上显示的所有建筑物的位置和高度,请编写一个程序以输出由这些建筑物形成的天际线(图B). ...
- [LeetCode] 281. The Skyline Problem 天际线问题
A city's skyline is the outer contour of the silhouette formed by all the buildings in that city whe ...
随机推荐
- VBA基本用法
Visual Basic for Applications 宏语言 打开VB编辑器 首先打开Excel,组合键Alt+F11 加载宏 找到相应的宏,点击"执行" 举例 Sub 评分 ...
- mac上配置apidoc环境
1. 安装node.js 和npm 前往 https://nodejs.org/en/ 下载node.js的最新版本,双击.pkg进行安装 在终端输入 node -v ,如正确输出版本号即安装成功 ( ...
- TouchGFX版本
TouchGFX 4.12.3版本 本文概述了TouchGFX 4.12.3版本,其总体功能以及如何与CubeMX和CubeIDE集成. 总览 有关4.12.3新增功能的一般概述,请查看发行版中的ch ...
- ASP.NET 静态化
以前也说过页面静态化 但是说的好像不清楚 这次我用一个插件 URLRewriter 重写URL 先引用dll 然后再web.config中三步走 <configSections> & ...
- Sourcetree 出现错误提示git -c diff.mnemonicprefix=false -c core.quotepath=false fetch origin
具体表现为:sourcetree无法和gitlab远程仓库进行交互,但使用本地cmd,可以使用git命令和远程仓库交互通过各种账户.秘钥等操作,都无法解决该问题具体信息如下: 解决方式:点击 工具–& ...
- ClassLoader源码分析与实例剖析
在之前已经对类加载器做了不少实验了,这次主要是来分析一下ClassLoader的源码,当然主要是先从理解官方给它的注释开始,为之后自定义类加载器打好坚石的基础,下面开始: 而从类的层次结构来看也能感受 ...
- golang struct的使用
Go struct tag深入理解 喜欢本站的朋友可以收藏本站,或者加入QQ群:172816590,我们大家一起来交流技术! 欢迎来到梁钟霖个人博客网站.本个人博客网站提供最新的站长新闻,各种互联网资 ...
- R中数据的输入和数据的标注
数据的导入 默认情况下数据导入时,字符型变量将转化为因子.若不希望转化,可设置 stringsAsFactors=FALSE 从带分隔符的文本文件中导入数据 read.table() file --& ...
- BZOJ3331 [BeiJing2013]压力[圆方树+树上差分]
圆方树新技能get.具体笔记见图连通性问题学习笔记. 这题求无向图的必经点,这个是一个固定套路:首先,一张连通的无向图中,每对点双和点双之间是以一个且仅一个割点连接起来的(如果超过一个就不能是割点了) ...
- [Sping Boot] Build a REST CRUD API with Hibernate
pom.xml: <dependencies> <dependency> <groupId>org.springframework.boot</groupId ...