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]
相关推荐
slacker-kian4 分钟前
[笔记]-什么是相似性搜索?
python·ai·向量·相似性搜索·点积·l2 距离·余弦相似度和距离
阿童木写作9 分钟前
跨境电商图片翻译工具,批量翻译视频字幕还免费
python·音视频
sukioe11 分钟前
城智连响:基于 LangGraph 与四库分层架构的城市公共设施智能报修与派单系统
人工智能·python·ai·架构·langchain
卷无止境1 小时前
Coding Agent 里的上下文 Compact,到底在压缩什么
后端·python
Patrick在香港1 小时前
Claude Prompt Caching 实测账单:第一轮贵 25%,从第二轮开始省 86%
python·prompt·claude·成本优化·prompt缓存·anthropic api
新时代牛马1 小时前
字符设备注册:从cdev_add 到chrdev_open 的 VFS路径
python
life1691 小时前
python requests采集同花顺F10、龙虎榜数据
python·同花顺·龙虎榜
angushine1 小时前
qwen3-tts使用实例
python·tts
玄昌盛不会编程2 小时前
LeetCode——2091. 从数组中移除最大值和最小值
java·算法·leetcode
陈年老古董2 小时前
OpenCV实战:文档扫描与图像风格迁移
人工智能·python·opencv·计算机视觉