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]
相关推荐
宸津-代码粉碎机5 小时前
Spring AI企业级实战|智能记忆摘要+自动遗忘机制落地,彻底解决上下文爆炸与Token冗余
java·大数据·人工智能·后端·python·spring
乘浪初心5 小时前
python调用API接口,免费API调取,学习如何调取API接口并反馈你输入的内容
开发语言·python·api·免费
AI玫瑰助手5 小时前
Python模块:import导入模块与模块的搜索路径
android·开发语言·python
傻啦嘿哟5 小时前
一篇文章讲清楚Python的变量作用域
开发语言·python
洛水水5 小时前
【力扣100题】84.字符串解码
算法·leetcode·职场和发展
装不满的克莱因瓶5 小时前
学习 LPRNet 框架——轻量级车牌识别网络从结构到工程落地
人工智能·python·深度学习·机器学习·ai
dust_and_stars5 小时前
Streamlit vs Gradio 完整对比
服务器·python
winfredzhang5 小时前
Python + wxPython + SQLite 实战:开发一个本地 Python 项目一键启动管理工具
python·sqlite·bat·截图·claudecode·codingliteplan
星越华夏5 小时前
python中四种获取文件后缀名的方法
开发语言·python
lunzi_08265 小时前
【学习笔记】《Python编程 从入门到实践》第9章:类、继承、组合与面向对象编程
笔记·python·学习