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 ...
随机推荐
- 使用nodejs对Marketing Cloud的contact主数据进行修改操作
假设在Marketing Cloud有这样一个contact主数据: 现在需求是使用编程语言比如nodejs修改这个contact实例的高亮属性. 代码如下: var config = require ...
- 02_Redis数据类型
Redis 数据类型:键值对存储 Redis支持五种数据类型:string(字符串),hash(哈希),list(列表),set(集合)及zset(sorted set:有序集合). 注意:key:全 ...
- 在SqlServer和Oralce中创建索引
给表名A的字段A增加索引 SqlServer: if exists (select 1 from sysobjects where name='表名A' and type='u')and exists ...
- java——springmvc——注册中央调度器
在WEB-INF下的web.xml中配置 <?xml version="1.0" encoding="UTF-8"?> <web-app xm ...
- linux——常用命令
学习linux命令地址: 学习命令地址,可参考http://linux.51yip.com/ 在文件中搜索指定字符串 grep -i "requirepass" redis.con ...
- ACM算法模板整理
史诗级ACM模板整理 基本语法 字符串函数 istream& getline (char* s, streamsize n ); istream& getline (char* s, ...
- gerapy+scrapyd组合管理分布式爬虫
Scrapyd是一款用于管理scrapy爬虫的部署和运行的服务,提供了HTTP JSON形式的API来完成爬虫调度涉及的各项指令.Scrapyd是一款开源软件,代码托管于Github上. 点击此链接h ...
- js实现QQ跳转到支付宝APP并领取红包!附:动图demo
前天我在sg开源了js实现微信跳转到支付宝并领红包的代码.https://segmentfault.com/a/11...于是朋友圈开始刷屏,各种套路,各种标题,再附上短链接,引起了很多人的好奇,然后 ...
- 3-cmd命令
1.查看IPC$是否启用 命令:net share 2.启动/停止windows服务 命令:net start MSDTC net stop MSDTC 3.修改服务的启动类型(start= ...
- 本地资源图片无法通过 WXSS 获取,可以使用网络图片,或者 base64,或者使用<image/>标签
在微信小程序开发中,当在CSS中使用背景图片格式为png时就会出现: 只要把png格式改掉就可以或者在<image/>标签里面写,我实测用JPG格式和把图片转成base64是没问题的.