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]
相关推荐
JaydenAI4 小时前
[拆解LangChain执行引擎] 基于Checkpoint的持久化
python·langchain
!chen4 小时前
线上服务重启后 nacos取不到配置
python
大连好光景5 小时前
GCN模型构建+训练+测试入门案例
pytorch·python·深度学习
大黄说说5 小时前
Go 实战 LeetCode 151:高效翻转字符串中的单词(含空格处理技巧)
开发语言·leetcode·golang
MSTcheng.5 小时前
【Leetcode二分查找】『在排序数组中查找元素的第一个和最后一个位置&搜索插入位置』
算法·leetcode·职场和发展
王老师青少年编程5 小时前
2021年信奥赛C++提高组csp-s初赛真题及答案解析(阅读程序第3题)
c++·题解·真题·初赛·信奥赛·csp-s·提高组
子午5 小时前
【海洋生物识别系统】Python+深度学习+人工智能+算法模型+图像识别+tensoflow+2026计算机毕设项目
人工智能·python·深度学习
七夜zippoe5 小时前
机器学习数学基础:线性代数与概率论深度解析
python·线性代数·机器学习·概率论·优化理论
有味道的男人5 小时前
除了Python,还有哪些语言可以调用1688商品详情API?
开发语言·python
Sheffield5 小时前
为什么Django这么慢,却还是Python后端第一梯队呢?
linux·python·django