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]
相关推荐
烂蜻蜓28 分钟前
Flask入门教程(七):请求与响应——Web应用的核心数据流
前端·python·flask
web行路人42 分钟前
AI 全栈学习之旅 - Week4:容器化部署与生产环境实战
python·学习
春涧草茶1 小时前
慢就是快 - 10-2函数 特殊字面量 返回值
python
慢云智慧空间1 小时前
空间智能与计算机视觉的本质差异:不止于识别,更是空间决策能力
人工智能·python·计算机视觉
典典分享指南1 小时前
Windows 自动化踩坑实录
python·intellij-idea·pyqt·emacs·visual studio
工业一体机老司机1 小时前
Python实现工业一体机OPC-UA通信:从SDK选型到数据采集实战
开发语言·python·单片机
威联通网络存储1 小时前
TS-h2477AXU-RP 在化工制造数据场景的部署
python·制造
wuyk5551 小时前
Python 零基础入门第七章:字典 Dict
开发语言·python
2601_966949651 小时前
如何判断盘中最新价相对昨日收盘涨跌幅,并实时触发止盈止损条件单:QuantDash Python 实战
开发语言·python·量化策略·量化·quantdash
土司大王2 小时前
LeetCode hot100——二叉树的最大深度
算法·leetcode·职场和发展