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]
相关推荐
码来的小朋友1 分钟前
手把手教你用 Python + PyQt5 做一个可视化图片切图工具
开发语言·python·microsoft
weixin199701080168 分钟前
[特殊字符]《京东订单API(jd.order.detail.get)对接ERP:企业认证+OAuth授权避坑指南》(附Python源码)
java·数据库·python
云烟成雨TD33 分钟前
LangFlow 1.x 系列【3】入门案例
人工智能·python·agent
创世宇图35 分钟前
【Python工程化实战】Python 服务的结构化日志体系:structlog + JSON 输出 + 日志分级策略
python·elk·structlog·结构化日志·可观测性
创世宇图1 小时前
【Python工程化实战】Kubernetes 中 Python 应用的优雅启停与健康检查:零停机滚动更新实战
python·云原生·kubernetes·优雅停机
夜雪一千1 小时前
Python 使用OpenAI调用Qwen3.6-27B-ms模型|完整参数详解
开发语言·python
zhiSiBuYu05172 小时前
重排序(Rerank)提升检索准确率实战指南
开发语言·python·算法
MageGojo2 小时前
集成企业工商信息查询API:从在线调试到生产级调用实战
python·调试·rest api·api集成·企业信息查询
huangjiazhi_2 小时前
Python3.14编写文件服务器
python
郭梧悠2 小时前
算法:有效的括号
python·算法·leetcode