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]
相关推荐
诸葛说抛光23 分钟前
国内大型汽车改装展览会定展 佛山改装 佛山汽车赛事
python·汽车
chouchuang31 分钟前
day-030-综合练习-笔记管理器
开发语言·笔记·python
乖巧的妹子37 分钟前
Python基础核心知识点详解:内置函数、运算符、字符串方法、数据结构与类型转换
python
幸福清风1 小时前
Python 完美处理Excel合并单元格:拆分填充+自动合并
python·excel·合并单元格·拆分单元格
汤姆小白2 小时前
08-应用部署
人工智能·python·机器学习·numpy·transformer
般若-波罗蜜3 小时前
MinerU高级用法,避坑指南(持续更新)
人工智能·python·语言模型·自然语言处理
用户8356290780513 小时前
使用 Python 对 PDF 文档进行数字签名的方法
后端·python
李可以量化4 小时前
PTrade 量化入门必学:get\_trading\_day 交易日函数全解与实战(下)
python
柒和远方4 小时前
LeetCode 139. 单词拆分 —— 从暴力回溯到 DP 完全背包
javascript·python·算法
半兽先生4 小时前
大模型实现金融文本分类
人工智能·python·金融