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]
相关推荐
合作小小程序员小小店19 小时前
web网页开发,在线%聚类,微博,舆情%系统,基于python,pycharm,django,nlp,kmeans,mysql
python·pycharm·kmeans·聚类·sklearn·kmean
Dan.Qiao19 小时前
python读文件readline和readlines区别和惰性读
开发语言·python·惰性读文件
闲人编程20 小时前
将你的旧手机变成监控摄像头(Python + OpenCV)
python·opencv·智能手机·监控·codecapsule·oasis
007php00720 小时前
大厂深度面试相关文章:深入探讨底层原理与高性能优化
java·开发语言·git·python·面试·职场和发展·性能优化
SunnyDays101120 小时前
Python 复制和移动 Excel 工作表并保留所有格式:详解
python·复制excel工作表·移动excel工作表·重新排列excel工作表
不会编程的小寒20 小时前
C++初始继承,继承中构造、析构顺序
开发语言·python
Mos_x21 小时前
关于我们的python日记本
开发语言·python
十重幻想21 小时前
reshape的共享内存
python
孙同学_21 小时前
面试题 16.25. LRU 缓存
leetcode·缓存
Juchecar21 小时前
设计模式不是Java专属,其他语言的使用方法
java·python·设计模式