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]
相关推荐
来酱何人6 分钟前
为什么要学深度学习?——从“传统编程”到“数据驱动”的思维跃迁(附AI落地案例)
人工智能·python·深度学习·机器翻译
程序员爱钓鱼7 分钟前
Python编程实战 - Python基础入门 - 容器的常用操作与应用
后端·python·bpython
熬了夜的程序员8 分钟前
【LeetCode】88. 合并两个有序数组
数据结构·算法·leetcode·职场和发展·深度优先
程序员爱钓鱼10 分钟前
Python编程实战 - 函数与模块化编程 - 函数的定义与调用
前端·后端·python
Dxy123931021619 分钟前
python如何做声音识别
开发语言·python
CodeCraft Studio26 分钟前
国产化Excel开发组件Spire.XLS教程:使用Python将CSV转换为XML(处理现实数据问题)
xml·python·excel·csv·spire.xls·csv转xml
合作小小程序员小小店34 分钟前
web开发,在线%高校舆情分析%系统demo,基于python,flaskweb,echart,nlp,ida,tf-idf,多爬虫源,数据库mysql
人工智能·python·flask·html5·tf-idf
骥龙2 小时前
1.2、实战准备:AI安全研究环境搭建与工具链
人工智能·python·安全
黄思搏2 小时前
Python + uiautomator2 手机自动化控制教程
python·智能手机·自动化
@LetsTGBot搜索引擎机器人2 小时前
Telegram 被封是什么原因?如何解决?(附 @letstgbot 搜索引擎重连技巧)
开发语言·python·搜索引擎·机器人·.net