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]
相关推荐
AI_RSER9 分钟前
Python 数据可视化全场景实现(一)
开发语言·人工智能·python·信息可视化·遥感
eqwaak012 分钟前
Matplotlib高阶技术全景解析(续):动态交互、三维可视化与性能优化
开发语言·python·语言模型·性能优化·交互·matplotlib
愚润求学16 分钟前
【专题四】前缀和(3)
开发语言·c++·笔记·leetcode·刷题·c++11
蜗牛沐雨19 分钟前
Pandas 数据导出:如何将 DataFrame 追加到 Excel 的不同工作表
python·excel·pandas
啊阿狸不会拉杆37 分钟前
人工智能数学基础(二):初等数学
人工智能·python·算法
元亓亓亓1 小时前
LeetCode热题100--560.和为K的子数组(前缀和)--中等
算法·leetcode·职场和发展
AI视觉网奇1 小时前
python 求内轮廓
python·opencv·计算机视觉
lczdyx1 小时前
从Flask到智能体:装饰器模式在AI系统中的架构迁移实践
人工智能·python·语言模型·架构·flask·装饰器模式
老胖闲聊1 小时前
Flask 请求数据获取方法详解
后端·python·flask
香蕉可乐荷包蛋1 小时前
Python面试问题
开发语言·python·面试