Ejemplo n.º 1
0
class MinMaxHeap:

    def __init__(self):
        self.min_pq = HashHeap()
        self.max_pq = HashHeap(key=lambda x, y: x > y)

    def __len__(self):
        return len(self.min_pq)

    def get_min(self):
        return self.min_pq.top()

    def get_max(self):
        return self.max_pq.top()

    def add(self, key):
        self.min_pq.add(key)
        self.max_pq.add(key)

    def pop_min(self):
        ans = self.min_pq.pop()
        self.max_pq.delete(ans)
        return ans

    def pop_max(self):
        ans = self.max_pq.pop()
        self.min_pq.delete(ans)
        return ans
 def test_of_add_and_delete(self):
     pq = HashHeap('min')
     array = random.sample(range(100000), 10000)
     for each in array:
         pq.add(each)
     array.sort()
     for i in range(10000):
         min = pq.top()
         self.assertEqual(min, array[i])
         pq.delete(min)
Ejemplo n.º 3
0
def building_outlines(buildings):
    points = []
    for b in buildings:
        start, end, height = b[0], b[1], b[2]
        points.append(Point(start, height, True))
        points.append(Point(end, height, False))
    points.sort()
    ans = []
    heap = HashHeap('max')
    start = 0
    for p in points:
        if p.is_start:
            if not heap or heap.top() < p.height:
                if heap:
                    ans.append([start, p.x, heap.top()])
                start = p.x
            heap.add(p.height)
        else:
            height = heap.top()
            heap.delete(p.height)
            if not heap or heap.top() < height:
                ans.append([start, p.x, height])
                start = p.x
    return ans