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]
相关推荐
学学学无无止境3 分钟前
力扣-从前序与中序遍历序列构造二叉树
leetcode
北极糊的狐13 分钟前
stream.findFirst().get() 报错 NoSuchElementException
开发语言·python
黑客思维者17 分钟前
Python数据清洗实战:去重/标准化
开发语言·python·数据清洗·数据标准化
CryptoRzz18 分钟前
对接印度股票市场数据 (India api) 实时k线图表
java·开发语言·python·区块链·maven
CoderYanger32 分钟前
第 479 场周赛Q1——3769. 二进制反射排序
java·数据结构·算法·leetcode·职场和发展
sin_hielo35 分钟前
leetcode 1925
数据结构·算法·leetcode
CoderYanger37 分钟前
A.每日一题——1925. 统计平方和三元组的数目
java·开发语言·数据结构·算法·leetcode·哈希算法
小白程序员成长日记39 分钟前
2025.12.08 力扣每日一题
java·算法·leetcode
鹿角片ljp1 小时前
基于 BiLSTM 的中文文本相似度计算项目实现
python·nlp·lstm