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]
相关推荐
清水白石0084 分钟前
NumPy 向量化实战指南:从原理到实践的性能革命
python·numpy
Coding茶水间17 分钟前
基于深度学习的猪识别系统演示与介绍(YOLOv12/v11/v8/v5模型+Pyqt5界面+训练代码+数据集)
图像处理·人工智能·python·深度学习·yolo·目标检测
X54先生(人文科技)37 分钟前
启蒙灯塔起源团预言—碳硅智能时代到来
人工智能·python·机器学习·语言模型
say_fall1 小时前
双指针算法详解:从原理到实战(含LeetCode经典例题)
算法·leetcode·职场和发展
qq_24218863321 小时前
快速搭建跨环境检测服务的步骤
linux·开发语言·windows·python·macos
追随者永远是胜利者1 小时前
(LeetCode-Hot100)33. 搜索旋转排序数组
java·算法·leetcode·职场和发展·go
JaydenAI1 小时前
[拆解LangChain执行引擎]三种持久化模式的差异
python·langchain
老赵全栈实战1 小时前
《从零搭建RAG系统第4天:问题向量化+Milvus检索匹配+结果优化》
python·ai编程
Katecat996631 小时前
【葡萄病害检测】基于SABL-RetinaNet的葡萄叶片黑腐病、霜霉病、白粉病和锈病自动识别系统
python
FL16238631292 小时前
windows从源码安装python版本paddleocr3.4.0
开发语言·windows·python