Python | Leetcode Python题解之第218题天际线问题

题目:

题解:

python 复制代码
class Solution:
    def getSkyline(self, buildings: List[List[int]]) -> List[List[int]]:
        buildings.sort(key=lambda bu:(bu[0],-bu[2],bu[1]))
        buildings.append([inf,inf,inf])
        heap = [[-inf,-inf,-inf]]
        ans = []
        for l,r,h in buildings:
            if heap[0][2] < l:
                topRight = heappop(heap)[2]
                while heap and topRight < l:
                    if heap[0][2] > topRight:
                        # 建筑群内有较较矮的建筑宽度比高的大,对齐进行切块
                        top = heappop(heap)
                        top[1], topRight = topRight, top[2]
                        # 如果新的切块也覆盖到新建筑,不需要再放入了
                        if topRight >= l: heappush(heap, top)
                        if ans[-1][1] != -top[0]: ans.append([top[1], -top[0]])
                    else: heappop(heap)
                if not heap: ans.append([topRight, 0])
            if not heap or h > -heap[0][0]: ans.append([l, h])
            if heap and heap[0][0] == -h and heap[0][2] < r: heap[0][2] = r
            else: heappush(heap, [-h,l,r])
        return ans[1:-1]
相关推荐
程序大视界8 分钟前
【Python系列课程】Pandas(六):数据读写——CSV与Excel文件操作
python·excel·pandas
weixin_4074438732 分钟前
OCR材料信息提取工具(附件中含代码和数据)
人工智能·python·计算机视觉·ocr
码农阿强37 分钟前
PixVerse 全系列视频生成模型技术架构详解 + Python 基于 StartAPI.top 接口实战调用
python·ai·架构·音视频·ai编程
Smilecoc1 小时前
风控评分卡模型原理与应用(四):WOE编码的单调性
python
凌波粒1 小时前
LeetCode--530.二叉搜索树的最小绝对差(二叉树)
算法·leetcode·职场和发展
许彰午1 小时前
04_Java数组操作全解
java·开发语言·python
废弃的小码农1 小时前
APP测试--adb使用介绍
python·测试工具·adb
曲幽1 小时前
你的FastAPI又在服务器上“跑不起来”了?来,今天咱把打包这件事彻底聊透
linux·windows·python·docker·fastapi·web·pyinstaller·nssm·services
AI玫瑰助手1 小时前
Python函数:局部变量与全局变量的作用域
开发语言·python·信息可视化
imDwAaY1 小时前
机器学习入门:从感知机到逻辑回归,理解线性分类器与Softmax CS188 Note20 学习笔记
人工智能·笔记·python·学习·机器学习·逻辑回归