标签:math iss 代码 == mat .com 高度 pos img
一、题目描述
给定建筑的轮廓坐标,求叠加之后的轮廓结果
二、解法
这个题目最容易想到的思路是扫描法
https://briangordon.github.io/2014/08/the-skyline-problem.html
但是这个方法用python3实现了之后,超时了。代码如下:
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轮廓高度,优化思路是在每一个顶点的时候,扫描包含该顶点的建筑。
按照上面的用例估计依然会超时,如果采用链表结果的插入排序的方式应该可以优化
先把上面的最大测试用例排除吧
标签:math iss 代码 == mat .com 高度 pos img
原文地址:https://www.cnblogs.com/doudouyoutang/p/10337906.html