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]
相关推荐
鬓戈31 分钟前
SeaweedFS集群上文件遍历和删除
运维·python
We་ct1 小时前
LeetCode 79. 单词搜索:DFS回溯解法详解
前端·算法·leetcode·typescript·深度优先·个人开发·回溯
眼眸流转1 小时前
LeetCode热题100(四)
算法·leetcode·职场和发展
困死,根本不会2 小时前
蓝桥杯python备赛笔记之(十)数论基础 & 日期问题
笔记·python·蓝桥杯
輕華2 小时前
Python 命令行参数处理:sys.argv 与 argparse 深度对比
python
王老师青少年编程2 小时前
2026年3月GESP真题及题解(C++一级):数字替换
c++·题解·真题·gesp·一级·2026年3月·数字替换
啊哦呃咦唔鱼2 小时前
LeetCode hot100-3 无重复字符的最长子串
算法·leetcode·职场和发展
清水白石0083 小时前
Python 内存陷阱深度解析——浅拷贝、深拷贝与对象复制的正确姿势
开发语言·python
国家二级编程爱好者3 小时前
删除typora文档没有引用的资源文件
git·python
进击的雷神3 小时前
邮箱编码解码、国际电话验证、主办方过滤、多页面深度爬取——柬埔寨塑料展爬虫四大技术难关攻克纪实
爬虫·python