def getSkyline(self, buildings): """ :type buildings: List[List[int]] :rtype: List[List[int]] """ if len(buildings) <= 0: return data = [[build[0], build[2], 0] for build in buildings] data += [[build[1], build[2], 1] for build in buildings] data = sorted(data, key=lambda x: (x[0], x[1])) print data heap = MaxHeap() print heap.size() result = [] for item in data: x, h, tag = item[0:] pre_height = -heap.top() if heap.size() > 0 else 0 if tag == 0: heap.add(-h) #top is the lowest height print 'push', -h else: print 'pop', heap.top() heap.pop() #pop the lowest height cur_height = -heap.top() if heap.size() > 0 else 0 if tag == 0 and cur_height > pre_height: result.append([x, h]) elif tag == 1 and h > cur_height: result.append([x, cur_height]) print item, pre_height, cur_height, result return result
def test_is_empty(self): heap = MaxHeap() assert heap.is_empty() is True heap = MaxHeap(capacity=10) assert heap.is_empty() is True heap.push(0) assert heap.is_empty() is False heap.pop() assert heap.is_empty() is True
def test_size(self): heap = MaxHeap() assert heap.size == 0 heap = MaxHeap(capacity=10) assert heap.size == 0 heap = MaxHeap([3, 2, 1]) assert heap.size == 3 heap.push(0) assert heap.size == 4 heap.pop() assert heap.size == 3 heap.pushpop(0) assert heap.size == 3 heap.replace(0) assert heap.size == 3
class RunningMedian: def __init__(self): self.left_max_heap = MaxHeap() self.right_min_heap = MinHeap() def stream_element(self, a): left_len, right_len = self.left_max_heap.get_length( ), self.right_min_heap.get_length() if left_len == right_len: self.left_max_heap.push(a) return if left_len > right_len: peek = self.left_max_heap.peek() if a >= peek: self.right_min_heap.push(a) else: popped = self.left_max_heap.pop() self.left_max_heap.push(a) self.right_min_heap.push(popped) return if left_len < right_len: peek = self.right_min_heap.peek() if a <= peek: self.left_max_heap.push(a) else: popped = self.right_min_heap.pop() self.right_min_heap.push(a) self.left_max_heap.push(popped) return def get_current_median(self): left_len, right_len = self.left_max_heap.get_length( ), self.right_min_heap.get_length() if left_len == 0 and right_len == 0: raise ("No element is streamed") if left_len > right_len: median = self.left_max_heap.peek() return median if right_len > left_len: median = self.right_min_heap.peek() return median if left_len == right_len: median = (self.left_max_heap.peek() + self.right_min_heap.peek()) / 2.0 return median
def test_pop(self): heap = MaxHeap() with pytest.raises(EmptyError): heap.pop() heap.push(1) assert heap.pop() == 1 heap.push(1) heap.push(2) heap.push(3) assert heap.pop() == 3 heap = MaxHeap([1, 2, 3]) assert heap.pop() == 3 assert heap.pop() == 2 assert heap.pop() == 1 with pytest.raises(EmptyError): heap.pop()
from max_heap import MaxHeap my_heap = MaxHeap() my_heap.heapify([8, 1, 5, 99, -12, 87]) print(f'my_heap init: {my_heap.heap}') my_heap.push(200) print(f'my_heap push: {my_heap.heap}') pop_element = my_heap.pop() print(f'my_heap pop: {my_heap.heap}, {pop_element}') my_heap.heapify([1, 2, 3, 4, 5, 6, 7]) print(f'my_heap redefine: {my_heap.heap}')