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]
相关推荐
程序员杰哥4 分钟前
如何做接口测试?
自动化测试·python·测试工具·职场和发展·测试用例·接口测试·postman
土司大王4 分钟前
LeetCode hot100——缺失的第一个正数
数据结构·算法·leetcode
weixin199701080167 分钟前
[特殊字符]《从0到1:闲鱼开放平台授权登录 + AccessToken 刷新 + 聚石塔部署完整链路》(附Python源码)
java·数据库·python
Cccp.12311 分钟前
【leetcode】(二)认识O(NlogN)的排序
算法·leetcode
布局呆星27 分钟前
RAG从0~1,初入~优化
python
王志来1379447300834 分钟前
场景驱动选型:4U工控机箱如何匹配多元化工业需求
大数据·人工智能·python
2601_962381581 小时前
[Python人工智能] 九.gensim词向量Word2Vec安装及《庆余年》中文短文本相似度计算
人工智能·python·tensorflow·word2vec·文本相似度
️学习的小王1 小时前
Flask实现云栖笔记|开箱即用网页版本地笔记本
笔记·python·flask
quantdash_cc1 小时前
批量请求和循环请求有什么区别?从 API 请求次数看量化数据获取的工程设计
开发语言·python·数据分析·pandas·量化交易·股票数据·quantdash
码农刚子1 小时前
告别影楼和付费 App,5 分钟本地搭一个证件照自由平台|HivisionIDPhotos 开箱实测
python·图像识别