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]
相关推荐
咕噜咕噜啦啦几秒前
ROS入门
linux·vscode·python
2301_79030096几秒前
用Matplotlib绘制专业图表:从基础到高级
jvm·数据库·python
爱尔兰极光6 分钟前
LeetCode--移除元素
算法·leetcode·职场和发展
XLYcmy9 分钟前
一个用于统计文本文件行数的Python实用工具脚本
开发语言·数据结构·windows·python·开发工具·数据处理·源代码
DFT计算杂谈24 分钟前
VASP+PHONOPY+pypolymlpj计算不同温度下声子谱,附批处理脚本
java·前端·数据库·人工智能·python
苦藤新鸡34 分钟前
51.课程表(拓扑排序)-leetcode207
数据结构·算法·leetcode·bfs
json{shen:"jing"}43 分钟前
js收官总概述
开发语言·python
双层吉士憨包1 小时前
Airbnb(爱彼迎)爬虫全流程解析:用 Python 抓取房源数据实战指南
python·ai·网络爬虫
VT.馒头1 小时前
【力扣】2694. 事件发射器
前端·javascript·算法·leetcode·职场和发展·typescript