首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >建筑物天际线

建筑物天际线
EN

Stack Overflow用户
提问于 2015-06-26 22:31:36
回答 4查看 876关注 0票数 0

我在努力理解天际线的问题。给定n矩形建筑,我们需要计算天际线。我很难理解这个问题的输出。

投入:(1,11,5),(2,6,7),(3,13,9),(12,7,16),(14,3,25),(19,18,22),(23,13,29),(24,4,28) } 输出天际线:(1,11),(3,13),(9,0),(12,7),(16,3),(19,18),(22,3),(25,0)

输出是对(xaxis, height)。为什么第三对(9,0)?如果我们看到天际线图,x轴值9的高度是13,而不是0.为什么要显示0?换句话说,如果我们使用第一个建筑(输入(1,11,5)),输出就是(1, 11)(5, 0)。你们能解释一下为什么是(5,0)而不是(5,11)吗?

EN

回答 4

Stack Overflow用户

发布于 2015-06-27 00:25:42

把屋顶的间隔想象为左边是封闭的,右边是开放的。

票数 0
EN

Stack Overflow用户

发布于 2015-06-27 07:12:52

您的输出并不表示“在x-高度为y",而是”在x-高度变化为y“。

票数 0
EN

Stack Overflow用户

发布于 2015-08-05 19:14:29

使用扫描行算法;下面是我的python版本解决方案:

代码语言:javascript
复制
class Solution:
    # @param {integer[][]} buildings
    # @return {integer[][]}
    def getSkyline(self, buildings):
        if len(buildings)==0: return []
        if len(buildings)==1: return [[buildings[0][0], buildings[0][2]], [buildings[0][1], 0]]

        points=[]
        for building in buildings:
            points+=[[building[0],building[2]]]
            points+=[[building[1],-building[2]]]
        points=sorted(points, key=lambda x: x[0])

        moving, active, res, current=0, [0], [],-1

        while moving<len(points):
            i=moving
            while i<=len(points):
                if i<len(points) and points[i][0]==points[moving][0]:
                    if points[i][1]>0:
                        active+=[points[i][1]]
                        if points[i][1]>current:
                            current=points[i][1]
                            if len(res)>0 and res[-1][0]==points[i][0]: 
                                res[-1][1]=current
                            else:
                                res+=[[points[moving][0], current]]
                    else:
                        active.remove(-points[i][1])
                    i+=1
                else:
                    break

            if max(active)<current:
                current=max(active)
                res+=[[points[moving][0], current]] 
            moving=i
        return res 
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31083076

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档