Hard-题目37:218. The Skyline Problem

LeetCode218. The Skyline Problem分治法 题意:轮廓问题,不好描述。首先怎么分: 不停对半分 停止条件1:为空时,直接返回空 停止条件2:大小为1时,直接返回左上角点和右下角点。 那么怎么合并? 维护两个变量:l,r l为左半部分当前位置,r为右半部分当前位置 维护两个变量:h1,h2 h1表示左半部分当前位置高度,h2表示右半部分档期位置高度 如果当前位置左半部分横坐标更小,就更新h1,从左半部分选元素; 如果当前位置右半部分横坐标更小, 阅读详情

题目原文:
A city’s skyline is the outer contour of the silhouette formed by all the buildings in that city when viewed from a distance. Now suppose you are given the locations and height of all the buildings as shown on a cityscape photo (Figure A), write a program to output the skyline formed by these buildings collectively (Figure B).
这里写图片描述
Buildings
这里写图片描述
Skyline Contour
The geometric information of each building is represented by a triplet of integers [Li, Ri, Hi], where Li and Ri are the x coordinates of the left and right edge of the ith building, respectively, and Hi is its height. It is guaranteed that 0 ≤ Li, Ri ≤ INT_MAX, 0 < Hi ≤ INT_MAX, and Ri - Li > 0. You may assume all buildings are perfect rectangles grounded on an absolutely flat surface at height 0.

For instance, the dimensions of all buildings in Figure A are recorded as: [ [2 9 10], [3 7 15], [5 12 12], [15 20 10], [19 24 8] ] .

The output is a list of “key points” (red dots in Figure B) in the format of [ [x1,y1], [x2, y2], [x3, y3], … ] that uniquely defines a skyline. A key point is the left endpoint of a horizontal line segment. Note that the last key point, where the rightmost building ends, is merely used to mark the termination of the skyline, and always has zero height. Also, the ground in between any two adjacent buildings should be considered part of the skyline contour.

For instance, the skyline in Figure B should be represented as:[ [2 10], [3 15], [7 12], [12 0], [15 10], [20 8], [24, 0] ].

Notes:

The number of buildings in any input list is guaranteed to be in the range [0, 10000].
The input list is already sorted in ascending order by the left x position Li.
The output list must be sorted by the x position.
There must be no consecutive horizontal lines of equal height in the output skyline. For instance, […[2 3], [4 5], [7 5], [11 5], [12 7]…] is not acceptable; the three lines of height 5 should be merged into one in the final output as such: […[2 3], [4 5], [12 7], …]
题目大意:
题干很长,就不直接翻译了。
大致是:有一些楼房,在平面直角坐标系上用矩形表示(其中y=0代表地面),按横坐标从小到大顺序求出这些大楼重叠起来的轮廓线的关键点(即可以描绘出轮廓的节点,在图B中用红点表示)。
例如,图A中的输入为:
2 9 10
3 7 15
5 12 12
15 20 10
19 24 8
输出(对应图B)为:
2 10
3 15
7 12
12 0
15 10
20 8
24 0

题目分析:
看着题目好长,而且暴力求解每一个x值对应的y值显然会超时,于是在discuss中找了一段神代码,是基于线段树的,大概意思是,先把区间离散化,然后在线段树中记录每个区间的最大高度,再遍历一遍线段树得到每个点的高度height[],,再把每个高度变化的点记录到最后返回的数组。
源码:(language:cpp)

#define left(i) (i<<1)

#define right(i) ((i<<1) + 1)

struct Node {
    int nodel, noder, nodeh;
};

struct Segment {
    int begin, end;
};

class Solution {
public:
    vector<pair<int, int>> getSkyline(vector<vector<int>>& buildings) {
        if(buildings.size() == 0) return {};
        int sz = buildings.size();
        vector<int> nums;
        for (int i = 0; i<sz; i++) {
            nums.push_back(buildings[i][0]);
            nums.push_back(buildings[i][1]);
        }
        sort(nums.begin(), nums.end());
        int pre = 0;
        int numssz = nums.size();
        map<int, int> m;
        for (int i = 0; i < numssz; i++) {
            if (pre < nums[i]) segments.push_back({ pre, nums[i] - 1 });
            segments.push_back({ nums[i], nums[i] });
            int idx = segments.size()-1;
            m[nums[i]] = idx;
            pre = nums[i]+1;
        }
        int segmentsnum = segments.size();
        tree = vector<Node>(segmentsnum << 2, { 0, 0, 0 });
        heights = vector<int>(segmentsnum+1, 0);

        buildTree(1, 0, segmentsnum);

        for (int i = 0; i<sz; i++) {
            set(1, m[buildings[i][0]], m[buildings[i][1]]-1, buildings[i][2]);
        }
        getHeights();
        vector<pair<int, int>> ret;
        for (int i = 0; i < segmentsnum; i++) {
            if (i == 0 && heights[i] != 0 || i != 0 && heights[i] != heights[i - 1]) ret.push_back(make_pair(segments[i].begin, heights[i]));
        }
        if (heights[segmentsnum - 1] != 0) ret.push_back(make_pair(segments[segmentsnum - 1].begin, 0));
        return ret;
    }
private:
    vector<Node> tree;
    vector<int> heights;
    vector<Segment> segments;
    void buildTree(int idx, int l, int r) {
        tree[idx].nodel = l;
        tree[idx].noder = r;
        tree[idx].nodeh = 0;
        if (l == r)
        {
            return;
        }
        int mid = (l + r) / 2;
        buildTree(left(idx), l, mid);
        buildTree(right(idx), mid + 1, r);
    }

    void update(int idx) {
        if (tree[idx].nodel == tree[idx].noder) return;
        if (tree[left(idx)].nodeh < tree[idx].nodeh) {
            tree[left(idx)].nodeh = tree[idx].nodeh;
        }
        if (tree[right(idx)].nodeh < tree[idx].nodeh) {
            tree[right(idx)].nodeh = tree[idx].nodeh;
        }
        tree[idx].nodeh = 0;
    }

    void set(int idx, int l, int r, int h) {

        if (tree[idx].nodel >= l && tree[idx].noder <= r) {
            tree[idx].nodeh = max(tree[idx].nodeh, h);
            return;
        }

        int mid = (tree[idx].nodel + tree[idx].noder) / 2;
        update(idx);
        if (mid >= l) {
            set(left(idx), l, r, h);
        }
        if (mid<r) {
            set(right(idx), l, r, h);
        }

    }

    void getHeights() {
        queue<int> q;
        q.push(1);
        while (!q.empty()) {
            int idx = q.front();
            q.pop();
            update(idx);
            if (tree[idx].nodel == tree[idx].noder)
            {
                heights[tree[idx].nodel] = tree[idx].nodeh;
                continue;
            }
            q.push(left(idx));
            q.push(right(idx));
        }
    }
};

成绩:
112ms,100%,836ms,10.74%
cmershen的碎碎念:
线段树在是acm中比较常见的数据结构,常用于区间查询和更改,但leetcode中涉及的不多。这道题的题解里面大多是用优先队列来做。

The Skyline Problem轮廓线问题算法详解 The Skyline Problem 阅读详情

相关推荐

The Skyline Problem解题心得

The Skyline Problem解题心得 原图链接:https://leetcode.com/problems/the-skyline-problem/description/ 题目复述 如下图所示,已知多个矩形建筑物的左右边界位置以及高度(建筑物都是贴地的)[Li, Ri, Hi] ;由此可见多个矩形建筑物在二维的平面上存在重叠的可能,在这个题目里我们需要求得重叠后的建

weixin_35816017的博客 1303

Codeforces Round #442 (Div. 2) 题解

题目链接:http://codeforces.com/contest/877 A. Alex and broken contest 题意:问 给定的串是否恰好只能匹配一个题上给出的人名。 解法:暴力匹配即可。 #include using namespace std; string a[5]={"Danil","Olya","Slava","Ann","Nikita"}; int mai

I good vegetable a! 376

【信息科学与工程学】【数据科学】数据科学领域 第六篇 算法设计 07 堆、栈、表、矩阵、集合、队列、数组、字符串、树、图、动态规划、结构体、元组的算法01

当前段必须包含所有字符的最后出现位置。1. 字符串分割成最多片段2. 基因序列功能域划分3. 文本段落划分4. 音频声道分离5. 图像色彩分区6. 社交网络社群划分7. 蛋白质结构域8. 音乐乐章分段9. 代码模块划分10. 章节划分特征:一次扫描,贪心确定边界。

weixin_49199313的博客 1292

一区即将要洗的DVD片子

101 Dalmatians [Animated] (2009, SE)《101斑点狗》预计2009年发行特别版12 Monkeys (05/10/2005, COM DOC)《12只猴子》预计2005年5月10日发行扩展版加评论和记录片等2001: A Space Odyssey (2006, SE)《2001:太空漫游》预计2006年发行特别版21 Grams (SE)《21克》特

现在的生活 521万+

商BC1590年世界历史地图矢量文件SHP格式.zip

本资源为《商BC1590年世界历史地图矢量文件SHP格式》压缩包,文件大小约 17.96 MB,便于集中下载、整理和使用。具体内容请结合压缩包内的实际文件与说明查看。 在当前可读取的目录中,可见属性表(.dbf)、投影说明文件(.prj)、Shapefile 主文件(.shp)、Shapefile 索引(.shx)。建议解压后保持原有目录结构,先浏览文件清单与说明,再选择需要的资料进行加载或整理。 应用方面,可根据实际资料用于学习练习、资料检索、研究整理或项目参考。使用时可先确定所需内容,建立清晰的文件分类,再按项目要求进行筛选、转换与汇总。 使用 Shapefile 数据时,请把实际配套的 .shp、.shx、.dbf 等文件保存在同一目录,并保持同名关联;加载后再检查坐标系与中文字段显示。如需叠加其它数据,请先统一坐标与字段口径。 数据来源、时间范围、坐标系、字段定义、精度和使用条件,请以实际文件及原始说明为准。本介绍不对文件名之外尚未核实的信息作额外承诺。

DeepSeek智慧政务数字政府AI大模型微调设计方案.docx

DeepSeek智慧政务数字政府AI大模型微调设计方案.docx

Codeforces Round #538 (Div. 2) C. Trailing Loves (or L'oeufs) 分解质因数

题解 题目大意 n!在b进制下末尾有多少个0 有一道经典例题n!在10进制下末尾有多少0,因为10=5*2且5和2都是质且5的个数肯定没2多,所以只需要统计n!中5有多少个即可 在b进制下先考虑可以被分解为质因数b=x*x*x*…y*y*y*…z*z*z… 如果n!中因子xyz…的数量都满足则在b进制下末尾一定有一个0 末尾0的个数就是(每个质因子数量/b中这个质因子数量)然后取min 再分解质因...

CaprYang的博客 416

大型央企十五五企业级数据编织(Data Fabric)架构与主动元数据管理平台初步设计方案.docx

大型央企十五五企业级数据编织(Data Fabric)架构与主动元数据管理平台初步设计方案.docx

hdu 5899 oasis in desert(acm/icpc 沈阳赛区网络赛,Floyd+二分图判定+最大匹配,好难啊)

传送门: http://acm.hdu.edu.cn/showproblem.php?pid=5899题目大意:沙漠中有N个绿洲,由M条路相连,每条路的长度为L。定义两个集合: (1)最大危险集:对于集合中任意两点,其最小距离大于k,满足此条件的集合有多个,取最大的那个。 (2)最小安全集:对于图中任意两点,只要他俩的距离≤k,那么最小安全集中至少包含这两个点中的一个。满足条件的集合有多个,取最

1456

圆铝杆:电网升级与高性能合金打开圆铝杆市场增长新空间.docx

圆铝杆:电网升级与高性能合金打开圆铝杆市场增长新空间

集团长任务Agent稳定性测试与故障恢复能力建设平台建设方案.docx

集团长任务Agent稳定性测试与故障恢复能力建设平台建设方案.docx

leetcode_c++:Divide and Conquer: The Skyline Problem(218)

题目class Solution { public: vector<pair<int, int>> getSkyline(vector<vector<int>>& buildings) { vector< pair<int, int> > edges; //put all of edge into a vector //set left edge

LandscapeMi 653

低空经济运营体系构建与实施路径规划方案.docx

低空经济运营体系构建与实施路径规划方案.docx

LeetCode218:The Skyline Problem

A city's skyline is the outer contour of the silhouette formed by all the buildings in that city when viewed from a distance. Now suppose you are given the locations and height of all the buildings as

3807

自动驾驶——最优控制算法(LQR)工程化总结

到近段时间,真正在我们的控制器上运行最优控制算法(LQR),一步一个脚印,从开始只是知道其“控制理论”的阶段、再到对着一堆网上的资料和公式一筹莫展不知何着手、再到初步实现了LQR算法的开发(但是使用的扭矩环控制,控制的不好,而且由于各种原因被delay,迟迟无法落地),直到大上个星期,泊车平台有此LQR开发需求,又重新了开始了算法的研发,用时一个周时间完成了算法开发,然后再用一个周时间进行实车最优控制算法验证与调试,取得了很大的进展,由于执行器是转角控制的,这无疑少了很多的工作量。

qq_40464599的博客 2795

XDS110 USB Debug Probe连接错误-260:CCS Theia环境下的诊断与修复指南

本文详细解析了CCS Theia环境下XDS110 USB Debug Probe连接错误-260的诊断与修复方法。从硬件连接到驱动状态、固件更新到软件配置,提供了全面的排查指南,帮助开发者快速解决这一常见的调试器通信障碍问题,确保嵌入式开发工作流畅进行。

hope8的博客 290

Lintcode 131. The Skyline Problem (Super Hard) (Python) (未完成)

The Skyline Problem Description: Given N buildings in a x-axis,each building is a rectangle and can be represented by a triple (start, end, height),where start is the start position on x-axis, end is ...

DY的博客 505

一步一步教你写股票走势图——K线图一(概述)

股票分时图部分已经开发完毕,受到了广大开发者的好评!界面、功能都挺完美,解决了好一些MP开发者的问题,博主甚是欣悦,更有动力去开源K线图。这里说一下,如果有问题直接留言或者发邮件,github上个人信息有邮件,我看到基本都会回复,博主不加qq的,体谅O(∩_∩)O。分析好了,废话不多说,我们分析下K线图吧,首先我们看下自选股K线图的效果吧效果还是很赞的,经过分时图的研究后,博主越来越确信自选股用的应

体验Android编程之美 3万+

leetcode 218: The Skyline Problem

leetcode 218: The Skyline Problem java python c++

西施豆腐渣 5857

深入解析luckyExcel处理xlsx与xls文件的实战技巧

本文深入解析了使用luckyExcel库在前端处理.xlsx与.xls文件的实战技巧。针对.xlsx文件,可直接调用API转换;对于老旧的.xls格式,则需结合SheetJS进行预处理。文章详细介绍了核心API的使用、两种格式的处理流程、数据提取方法以及性能优化建议,为开发在线Excel编辑应用提供了完整解决方案。

cherry的博客 824

python爬取《你好, 李焕英》豆瓣评论数据

# 导入工具包 import requests from bs4 import BeautifulSoup import time import pandas as pd import numpy as np # 请求头 headers={'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.190 Safari/537.36'} # =

python技巧分享 3465

UE4星空背景

UE4做星空 找了几个帖子,要钱的,我,看不起.我始终坚信,if you real love it,set it free,so,you reall love it very much?No,you just love Unreal Money! 先来看下我做的场景:魔法森林~(莫打我...) 你知道吗,我在让15虚幻币的东西Set Free.学长只能帮你们到这儿了,所以,大神们,多多努

迷雾森林的专栏 1万+

拓竹P1S封神玩法|AI图生3D+精准修改全攻略,打孔/挖盖再也不用瞎琢磨!

AI图生3D建模结合后期修改,让拓竹P1S用户轻松实现创意打印。文章详细介绍了从照片拍摄、AI模型生成到后期修改的全流程:使用腾讯混元3D或Tripo3D快速生成3D模型,通过Blender或Fusion360进行精准打孔、挖盖子等修改,最后在OrcaSlicer中优化切片参数适配P1S打印。重点解决了AI模型无法直接打印的问题,提供了壁厚控制、孔径调整等实用技巧,并针对常见打印问题给出解决方案,帮助新手实现从AI生成到成品打印的无缝衔接。

2501_93651177的博客 1938

高清无码的2018全球区块链3.0产业图谱

2008 年,中本聪发表奠基性论文《Bitcoin: A Peer-to-Peer Electronic Cash System》,“区块链”这一概念横空出世。区块链技术具有去中介化、可追溯性好、难以篡改等特点,鉴于其在金融、科技等领域深刻的颠覆式变革潜力。近年来,区块链受到全球的广泛关注,我这里有一份区块链的通俗解释版,详情见《从区块链看三国群英榜,万年老二叫得不冤!》。从应用角度,业内普遍将区...

cpongo011702 12万+

1、2022 应用密码学与网络安全会议亮点与关键技术解读

2022年应用密码学与网络安全会议(ACNS 2022)在意大利罗马成功举办,会议涵盖了密码学与网络安全的多个前沿领域,包括键控全同态加密、隐私保护技术、区块链、后量子密码学和多方计算等。会议共收录44篇论文,展示了众多创新的研究成果,为学术研究和实际应用提供了新的思路和方法。本次会议促进了学术界和工业界的交流与合作,推动了密码学与网络安全行业的发展。

blue的专栏 183

高级数据结构之R树(R-tree)

R树(R-tree)是一种将B树扩展到多维情况下得到的数据结构,它最初由Antonin Guttman于1984年提出。B树的结点中会存储一个键的集合,这些键把线分成片段,沿着那条线的点仅属于一个片段。因此,B树使得我们可以很容易地找到点。如果把沿线各处的点表示成B树结点,我们就能够确定点所属唯一子结点,在那里可以找到该点。 Antonin Guttman在他提出R树的经典论文中给出的R树例子...

白马负金羁 1万+
上一篇: Hard-题目36:32. Longest Valid Parentheses
下一篇: Hard-题目38:188. Best Time to Buy and Sell Stock IV
无码萌妹码代码
博客等级 码龄11年 105粉丝 281原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值