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.
注意事项
请注意合并同样高度的相邻轮廓,不同的轮廓线在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