每天两道LeetCodeHard:(32)

189 阅读1分钟

218. The Skyline Problem

又是skyline

题干:

A city's skyline is the outer contour of the silhouette formed by all the buildings in that city when viewed from a distance. Now suppose you are given the locations and height of all the buildings as shown on a cityscape photo (Figure A), write a program to output the skyline formed by these buildings collectively (Figure B).

解释:

给了所有大楼的左右边界和高度,让我们求每个点对应的最大高度

思考:

其实很多hard题难就难在,无法把具体的问题抽象成我们解决过的经典问题。这个题本质还是扫描时间线,每个楼代表了一个修改动作,一个常量代表边界线,最后输出每个点的最大值即可。

答案:

class Solution(object):
    def getSkyline(self, buildings):
        # 把左边界和右边界的坐标保留下来
        x = [i[0] for i in buildings] + [i[1] for i in buildings]
        # 排序
        x.sort()
        index = 0
        heap = []
        res = [[0, 0]]
        # 从小到大的顺序循环边界的值
        for i in x:
            # index表示的是建筑的编号,找到建筑左边界等于i的建筑放到大根堆
            while index < len(buildings) and buildings[index][0] == i:
                # 大根堆存放的是(高,右边界)
                heapq.heappush(heap, (-buildings[index][2], buildings[index][1]))
                # 建筑编号加1
                index += 1

            # 大根堆的堆顶元素即建筑的最高值,如果这栋建筑的右边界小于等于i,即该建筑已经遍历完了,不需要了,则从堆中移出
            while heap and heap[0][1] <= i:
                heapq.heappop(heap)
            # 如果堆里有值,把堆顶的元素的高取出来
            h = -heap[0][0] if heap else 0
            # 和结果中的高进行对比,如果不相同,说明不在一条直线上,把该值添加到res中
            if h != res[-1][1]:
                res.append([i, h])

        return res[1:]

答案补充: