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]
相关推荐
min(a,b)43 分钟前
学习第 4 天:面向对象与异常处理
python·学习·学习方法
yangshicong2 小时前
第19章:AI安全防护与AI安全
人工智能·python·安全·prompt·ai编程
果汁华2 小时前
Function Calling 与 Python 实战完整指南
开发语言·网络·python
c_lb72883 小时前
2026年不同基础做量化,先找AI能参与的位置
人工智能·python
chenment4 小时前
ComfyUI 自定义节点开发:从零扩展你的图像生成工作流
python·stable diffusion
IT空门:门主5 小时前
Python 基础语法学习路线图
网络·python·学习
yyds_yyd_100865 小时前
1464. 数组中两元素的最大乘积(2026.07.27)
数据结构·c++·算法·leetcode
互联网中的一颗神经元5 小时前
小白python入门 - 25. SQL 与表设计入门
数据库·python·sql
RSTJ_16257 小时前
PYTHON+AI LLM DAY ONE HUNDRED AND EIGHTEEN
python
开源量化GO7 小时前
近期量化学习路径,交易理解和技术实现要接上
人工智能·python