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]
相关推荐
Python自动化直播1 天前
智能脚本批量管理拆开看:导入、导出、重写、试听
前端·python·ai·秒播
春弦_1 天前
LeetCode Hot 100 刷题笔记
笔记·leetcode·哈希算法
伤心的梧桐叶1 天前
Picset与创客贴比等待时间,没做完的任务不能从表里消失
python
没有气的汽水1 天前
一次 SQS 消费者静默退出的排查:问题最后竟然出在日志里
python
用户450030140231 天前
物业催缴系统为什么总在"冷却"这一步翻车:账期建模与规则命中的几个取舍
python
Web极客码1 天前
决策模型 + LLM:用 Python 拆解浏览器自动化工作流
java·python·自动化
fanruitian1 天前
python 装饰器 闭包
python
凯瑟琳.奥古斯特1 天前
力扣1015C++解法技巧
c++·算法·leetcode·链表·职场和发展
泡茶喝茶写代码1 天前
A股量化策略回测实战:从行情数据到绩效评估(第 13 篇):放量突破策略
java·python·股票数据api·股票数据api接口·股票量化数据api·股票量化数据接口
文丑颜不良啊1 天前
LeetCode626~650题目解析
java·leetcode