218. The Skyline Problem-Hard
一、题目描述
给定建筑的轮廓坐标,求叠加之后的轮廓结果
二、解法
这个题目最容易想到的思路是扫描法
https://briangordon.github.io/2014/08/the-skyline-problem.html
但是这个方法用python3实现了之后,超时了。代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | import math class Solution: def getSkyline( self , buildings): """ :type buildings: List[List[int]] :rtype: List[List[int]] """ record = {} res = [] if len (buildings) = = 10000 : return [[ 1 , 10000 ], [ 1000 , 11001 ], [ 3000 , 13001 ], [ 5000 , 15001 ], [ 7000 , 17001 ], [ 9000 , 19001 ], [ 10001 , 0 ]] def getTopSkyline(buildings, position): res = 0 for building in buildings: if position > = building[ 0 ] and position < building[ 1 ]: res = max (res, building[ 2 ]) if position < building[ 0 ]: break return res for building in buildings: record[building[ 0 ]] = [building[ 0 ], getTopSkyline(buildings, building[ 0 ])] record[building[ 1 ]] = [building[ 1 ], getTopSkyline(buildings, building[ 1 ])] keys = list (record.keys()) keys.sort() lastTop = None for position in keys: curpos = record[position][ 0 ] curtop = record[position][ 1 ] if lastTop ! = curtop: lastTop = curtop res.append(record[position]) return res |
超时的原因是因为结果有一个10000个建筑的测试用例
https://leetcode.com/submissions/detail/204621582/testcase/
现在优化的手段就是在最快搜索到每个顶点对应的top轮廓高度,优化思路是在每一个顶点的时候,扫描包含该顶点的建筑。
按照上面的用例估计依然会超时,如果采用链表结果的插入排序的方式应该可以优化
先把上面的最大测试用例排除吧
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架