LintCode 131 大楼轮廓

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

 注意事项

请注意合并同样高度的相邻轮廓,不同的轮廓线在x轴上不能有重叠。

样例

给出三座大楼:

[
  [1, 3, 3],
  [2, 4, 4],
  [5, 6, 1]
]

外轮廓线为:

[
  [1, 2, 3],
  [2, 4, 4],
  [5, 6, 1]
]


这个题目现在还没过!!!!还没过!!!

暂且记录下来,防止遗忘。


思路倒是简单,但是运行到77%的用例的时候超时了!!!

然后手写了一个堆,还是超时!

然后修改了一下堆,因为用到的数据只有堆顶,其余的数据没有必要有序,而堆排序需要O(nlog(n)),所以只需要维持堆顶最大即可,删除堆顶之后遍历找到最大值,赋值给堆顶,O(n)。但是还是超时!悲伤(╮(╯▽╰)╭)

有可能是排序或者代码思路的问题,先记下来吧。。


class myHeap(object):
    def __init__(self):
        self.heap = []
        self.length = 0
    #添加一个元素
    def add(self, item):
        self.heap.append(item)
        self.length += 1
        if item > self.heap[0]:
            self.heap[0], self.heap[self.length - 1] = self.heap[self.length - 1], self.heap[0]

    #调整堆
    def adjust_heap(self, i, size): 
        lchild = 2 * i + 1
        rchild = 2 * i + 2
        max = i
        tmp = self.heap[i]
        while i < int(size / 2):
            if lchild < size and self.heap[lchild] > self.heap[max]:
                max = lchild
            if rchild < size and self.heap[rchild] > self.heap[max]:
                max = rchild
            if max != i:
                self.heap[max], self.heap[i] = self.heap[i], self.heap[max]
                i = max
                lchild = 2 * i + 1
                rchild = 2 * i + 2
            else:
                self.heap[i] = tmp
                break
    #得到最大元素
    def gettop(self):
        return self.heap[0]
    #删除最大元素
    def removetop(self):
        res = self.heap[0]
        self.heap.pop(0)
        self.length -= 1
        if self.length > 0:
            maxv = 0
            maxindex = 0
            for index, v in enumerate(self.heap):
                if v > maxv:
                    maxv = v
                    maxindex = index
            self.heap[0], self.heap[maxindex] = self.heap[maxindex], self.heap[0]

        # for i in range(0, int(self.length / 2))[::-1]:
        #     self.adjust_heap(i, self.length)
        return res
    def remove(self, item):
        if item == self.heap[0]:
            self.removetop()
        else:
            for index, v in enumerate(self.heap):
                if v == item:
                    self.heap.pop(index)
                    self.length -= 1
                    break
    #统计元素是否只有一个
    def one(self, item):
        count = 0
        for v in self.heap:
            if v == item:
                count += 1
            if count == 2:
                break
        return count == 1
    #判断堆是否为空
    def empty(self):
        return self.length == 0

class Solution:
    # @param buildings: A list of lists of integers
    # @return: A list of lists of integers
    def buildingOutline(self, buildings):
        # buildings = sorted(buildings,key=lambda x:x[0])
        edgelist = []#边的list
        reslist = []#结果的list
        highnum = {}#记录高度的个数
        for i in range(0, 1500):
            highnum[i] = 0
        heap = myHeap()#最大堆
        for temp in buildings:
            edgelist.append((temp[0], 'start', temp[2]))
            edgelist.append((temp[1], 'end', temp[2]))
        edgelist = sorted(edgelist, key=lambda x: (x[0], -x[2]))
        for i, edge in enumerate(edgelist):
            #如果heap中内容为空,说明是初始情况或者遇到了独立的新楼,重新开始计算
            if heap.empty():
                startindex = edge[0]
                heap.add(edge[2])
                highnum[edge[2]] = 1
                continue
            tophigh = heap.gettop()
            if edge[1] == 'start':
                heap.add(edge[2])
                highnum[edge[2]] += 1
                if edge[2] > tophigh and startindex < edge[0]:
                    reslist.append([startindex, edge[0], tophigh])
                    startindex = edge[0]
            elif edge[1] == 'end':
                if edge[2] == tophigh and startindex < edge[0]:
                    if highnum[tophigh] == 1:  # 大楼高度相同,互相交叉,只算最外层轮廓!!
                        reslist.append([startindex, edge[0], edge[2]])
                        startindex = edge[0]
                # if edge[1] == 'end':
                heap.remove(edge[2])
                highnum[edge[2]] -= 1
        return reslist


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值