当前位置:网站首页>力扣(LeetCode)218. 天际线问题(2022.08.06)
力扣(LeetCode)218. 天际线问题(2022.08.06)
2022-08-07 03:10:00 【ChaoYue_miku】
城市的 天际线 是从远处观看该城市中所有建筑物形成的轮廓的外部轮廓。给你所有建筑物的位置和高度,请返回 由这些建筑物形成的 天际线 。
每个建筑物的几何信息由数组 buildings 表示,其中三元组 buildings[i] = [lefti, righti, heighti] 表示:
lefti 是第 i 座建筑物左边缘的 x 坐标。
righti 是第 i 座建筑物右边缘的 x 坐标。
heighti 是第 i 座建筑物的高度。
你可以假设所有的建筑都是完美的长方形,在高度为 0 的绝对平坦的表面上。
天际线 应该表示为由 “关键点” 组成的列表,格式 [[x1,y1],[x2,y2],…] ,并按 x 坐标 进行 排序 。关键点是水平线段的左端点。列表中最后一个点是最右侧建筑物的终点,y 坐标始终为 0 ,仅用于标记天际线的终点。此外,任何两个相邻建筑物之间的地面都应被视为天际线轮廓的一部分。
注意:输出天际线中不得有连续的相同高度的水平线。例如 […[2 3], [4 5], [7 5], [11 5], [12 7]…] 是不正确的答案;三条高度为 5 的线应该在最终输出中合并为一个:[…[2 3], [4 5], [12 7], …]
示例 1:
输入:buildings = [[2,9,10],[3,7,15],[5,12,12],[15,20,10],[19,24,8]]
输出:[[2,10],[3,15],[7,12],[12,0],[15,10],[20,8],[24,0]]
解释:
图 A 显示输入的所有建筑物的位置和高度,
图 B 显示由这些建筑物形成的天际线。图 B 中的红点表示输出列表中的关键点。
示例 2:
输入:buildings = [[0,2,3],[2,5,3]]
输出:[[0,3],[5,0]]
提示:
1 <= buildings.length <= 104
0 <= lefti < righti <= 231 - 1
1 <= heighti <= 231 - 1
buildings 按 lefti 非递减排序
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/the-skyline-problem
方法一:扫描线+优先队列
C++提交内容:
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)> que(cmp);
vector<int> boundaries;
for (auto& building : buildings) {
boundaries.emplace_back(building[0]);
boundaries.emplace_back(building[1]);
}
sort(boundaries.begin(), boundaries.end());
vector<vector<int>> ret;
int n = buildings.size(), idx = 0;
for (auto& boundary : boundaries) {
while (idx < n && buildings[idx][0] <= boundary) {
que.emplace(buildings[idx][1], buildings[idx][2]);
idx++;
}
while (!que.empty() && que.top().first <= boundary) {
que.pop();
}
int maxn = que.empty() ? 0 : que.top().second;
if (ret.size() == 0 || maxn != ret.back()[1]) {
ret.push_back({
boundary, maxn});
}
}
return ret;
}
};
边栏推荐
猜你喜欢

Basic usage based on 7.6ElstaicSearch syntax

Dangerous, please replace BeanUtils in the code immediately

基于STM32F103C8T6最小系统板驱动灰度模块进行循迹

什么是(UL)SIS线?

损失函数_相似度计算_距离计算

Wechat applet's homestay room reservation uniapp applet
![Rasa 3.x Learning Series - Rasa [3.2.5] - 2022-08-05 New version released](/img/db/4fd216ae8e2278cc640ce20501f2b2.png)
Rasa 3.x Learning Series - Rasa [3.2.5] - 2022-08-05 New version released

Large website high concurrency solution - cluster

Large website high concurrency solution - cluster

coco数据集解析及读取方法
随机推荐
POST request
Dangerous, please replace BeanUtils in the code immediately
LVS+Keepalived
406. According to height reconstruction queue - sort + dynamic programming
pytorch: dataloader custom data set production
Getting Started with Tensorflow 2.0
使用软引用实现缓存机制
haproxy实验
Auto.js实现视频号点赞自动化
WeChat applet's campus job search and recruitment system uniapp with source code
laravel查询作用域
Rasa 3.x Learning Series - Rasa [3.2.5] - 2022-08-05 New version released
ModuleNotFoundError: No module named ‘sentence_transformers‘
基于STM32F103C8T6最小系统板驱动灰度模块进行循迹
X 进制减法
【Jmeter生成测试报告】
coco数据集解析及读取方法
【栈、队列与二叉树】
JUC源码学习笔记4——原子类,CAS,Volatile内存屏障,缓存伪共享与UnSafe相关方法
致所有测试员:一面二面都过了,千万别栽在HR面——70道HR面试题分享