Пример #1
0
class BuildingMap:
    def __init__(self):
        self.map = defaultdict(list)
        self.heap = MaxHeap()

    def put(self, height):
        self.heap.push(height)
        self.map[height].append(height)

    def remove(self, height):
        if height not in self.map:
            print(f'height {height} not in map')
            return

        n = self.map[height].pop()

        self.heap.remove(n)

        if len(self.map[height]) == 0:
            self.map.pop(height)

    def getMax(self):
        if self.heap.empty():
            return 0
        return self.heap.peek()
Пример #2
0
    def test_max_heap_max_is_retained(self):
        maxHeap = MaxHeap()
        maxHeap.push(7)
        maxHeap.push(4)
        maxHeap.push(10)
        maxHeap.push(6)
        maxHeap.push(5)

        self.assertEqual(10, maxHeap.peek())
        maxHeap.remove(10)
        self.assertEqual(7, maxHeap.peek())
        maxHeap.remove(6)
        self.assertEqual(7, maxHeap.peek())