Lintcode 131. The Skyline Problem (Super Hard) (Python) (未完成)

本文深入探讨了天际线问题,这是一个经典的计算机科学问题,涉及到处理一组建筑物并找出它们的轮廓线。通过给出具体的示例和代码实现,文章详细解释了如何通过分析建筑物的位置和高度来确定最终的轮廓线。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

The Skyline Problem

Description:

Given N buildings in a x-axis,each building is a rectangle and can be represented by a triple (start, end, height),where start is the start position on x-axis, end is the end position on x-axis and height is the height of the building. Buildings may overlap if you see them from far away,find the outline of them。

An outline can be represented by a triple, (start, end, height), where start is the start position on x-axis of the outline, end is the end position on x-axis and height is the height of the outline.

Building Outline

Example
Given 3 buildings:

[
[1, 3, 3],
[2, 4, 4],
[5, 6, 1]
]
The outlines are:

[
[1, 2, 3],
[2, 4, 4],
[5, 6, 1]
]
Notice
Please merge the adjacent outlines if they have the same height and make sure different outlines cant overlap on x-axis.

Code:

1. 空间复杂度过高

import heapq
class Solution:
    """
    @param buildings: A list of lists of integers
    @return: Find the outline of those buildings
    """
    def buildingOutline(self, buildings):
        # write your code here
        if not buildings:
            return []
        res = []
        print(res)
        area = [0]*max(max(_) for _ in buildings)
        for i in buildings:
            for j in range(i[0], i[1]):
                area[j-1] = max(area[j-1], i[2])
        start, end = 1, 2
        print('area', area)
        while end<=len(area):
            
            while end<len(area) and area[start-1]==area[end-1]:
                print('while', start, end)
                end += 1
            
            if area[start-1]!=0:
                print('startend', start, end)
                res.append([start, end, area[start-1]])
            start = end
            end += 1
            print(res)

        return res
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值