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]
相关推荐
好学且牛逼的马26 分钟前
从“混沌初开”到“有序统一”:Java集合框架发展历程与核心知识点详解
前端·数据库·python
a11177634 分钟前
快速制作 虚拟形象项目 MotionPNGTuber
python·live2d
一切尽在,你来36 分钟前
AI大模型应用开发前置知识:Python迭代器和生成器深入详解
python·langchain·ai编程
小雨中_1 小时前
2.7 强化学习分类
人工智能·python·深度学习·机器学习·分类·数据挖掘
摩拜芯城IC2 小时前
ATSHA204A‑STUCZ CryptoAuthentication 安全认证芯片IC
python·安全
We་ct2 小时前
LeetCode 226. 翻转二叉树:两种解法(递归+迭代)详解
前端·算法·leetcode·链表·typescript
小雨中_2 小时前
2.4 贝尔曼方程与蒙特卡洛方法
人工智能·python·深度学习·机器学习·自然语言处理
追随者永远是胜利者3 小时前
(LeetCode-Hot100)64. 最小路径和
java·算法·leetcode·职场和发展·go
MediaTea3 小时前
Python:可迭代对象(对象语义角色)
开发语言·python
skywalk81633 小时前
Diffusers 库介绍,它支持LTX-2模型
python