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]
相关推荐
吴佳浩11 分钟前
Python入门指南-AI模型相似性检测方法:技术原理与实现
人工智能·python·llm
叶 落20 分钟前
计算阶梯电费
python·python 基础·python 入门
Python大数据分析@1 小时前
Origin、MATLAB、Python 用于科研作图,哪个最好?
开发语言·python·matlab
qq_513970441 小时前
力扣 hot100 Day37
算法·leetcode
编程零零七2 小时前
Python巩固训练——第一天练习题
开发语言·python·python基础·python学习·python练习题
不見星空2 小时前
leetcode 每日一题 1865. 找出和为指定值的下标对
算法·leetcode
Zonda要好好学习2 小时前
Python入门Day4
java·网络·python
小龙在山东3 小时前
Python 包管理工具 uv
windows·python·uv
weixin_307779133 小时前
批量OCR的GitHub项目
python·github·ocr
孤狼warrior4 小时前
灰色预测模型
人工智能·python·算法·数学建模