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 分钟前
FA_CNN卷积神经网络四---线性层及其拉平PyTorch
人工智能·pytorch·python·深度学习·神经网络·cnn
钱栈up9 分钟前
GitHub热榜上手|Claude插件:4.3万星官方目录
python·开源·github
青春不朽51212 分钟前
Python 的 list append 为什么是均摊 O(1)?拆开过度分配那张表,看容量翻倍背后的暗账
java·python·list
小羊没烦恼!1 小时前
.NET 11 Runtime Async 详解
java·运维·开发语言·javascript·python
染指11101 小时前
152.Agent-LangGraph-Command的参数
人工智能·python·langchain·agents
傅科摆 _ py1 小时前
链表力扣复习
数据结构·leetcode·链表
第404号工位2 小时前
“已彻底删除”的照片与文件到底去哪了?我用底层扇区扫描,扒出手机和电脑的幽灵数据
linux·python·架构
智购科技自动售卖机厂家2 小时前
双摄像头方案F1-score只有0.85,排查发现是上下视角的融合逻辑写反了~YH
python·单片机·嵌入式硬件·算法·eclipse
田里的水稻2 小时前
FA_CNN卷积神经网络二---PyTorch
人工智能·pytorch·python·深度学习·cnn
小肝一下2 小时前
1.大模型介绍
python·langchain·大模型·ollama·langgrap