当前位置:网站首页>LeetCode 218. Skyline Issues (2022.08.06)
LeetCode 218. Skyline Issues (2022.08.06)
2022-08-07 03:38: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;
}
};
边栏推荐
- 建筑工地无线视频监控 工业级无线路由器应用
- 微信小程序的民宿客房预订uniapp小程序
- [Swift] Add copy function to custom object
- 【RF】Radio Frequency Integrated Circuit and System Design
- 记录WPF的技巧(二)16-30
- 微信小程序当input框中的值改变时获取input框的值
- FutureTask源码深度剖析
- 2333. Minimum difference sum of squares - sorting and binary search
- The second virtual camera: configure the v4l2loopback virtual camera as the front or rear camera
- 【LeetCode每日一题】——34.在排序数组中查找元素的第一个和最后一个位置
猜你喜欢
随机推荐
Intelligent street lamp intelligent remote control
Wechat applet online learning daily check-in and punch-in project source code introduction
方舟生存进化游戏设置怎么调设置推荐
LVS-DR
activiti7入门教程
基于STM32F103C8T6最小系统板驱动灰度模块进行循迹
【LeetCode每日一题】——153.寻找旋转排序数组中的最小值
力扣(LeetCode)218. 天际线问题(2022.08.06)
c#多线程同步执行
记录WPF的技巧(二)16-30
LVS load balancing cluster
水质自动监测和视频监控,巩固提升饮用水安全保障水平
The difference between target and currentTarget in WeChat applet
Implement caching mechanism using soft references
Small application container in the application of integrated online government service platform
POST request
golang服务器代理
【Metaverse系列一】元宇宙的奥秘
Definition and operation process of OAuth2
面试字节跳动测试岗被吊打,60天苦修这些笔记,侥幸收获offer








