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]
相关推荐
夏季疯42 分钟前
读论文:STARS 是什么结构?一个统一的歌声自动标注框架
python
鱼儿也有烦恼1 小时前
快速学完 LeetCode top 1~50
leetcode·algorithm
光测实验室2 小时前
3种Python降噪算法实测:我把处理速度提升了20倍
python
AOwhisky2 小时前
Python 学习笔记(第三期)——流程控制核心知识点自测与详解
开发语言·笔记·python·学习·云原生·运维开发·流程控制
花花无缺3 小时前
Windows 定时执行 Python 脚本方案
python·操作系统·命令行
流云鹤4 小时前
1. 配置环境、创建导航栏
python·django
三亚兴嘉装饰4 小时前
准备在三亚装房子找哪家装修
python
Mx_coder4 小时前
8年Java开发者AI转型第二周:RAG系统深入 + 向量数据库实战(Day 8-10)
python
ximen502_5 小时前
Python 语言知识总结
开发语言·python
流云鹤5 小时前
2.登录模块
python·django