示例#1
0
def median_maintenance(array):
    medians = []
    heap_low = MaxHeap([])
    heap_high = MinHeap([])
    for item in array:
        median = median_in_time(item, heap_low, heap_high)
        medians.append(median)
    return medians
 def mst_prim(self):
     root.key = self.nodes[0]
     Q = MinHeap(self.nodes)
     while not Q.is_empty():
         u = Q.extract_min()
         for v in self.adj_list[u]:
             if v in Q and self.weight[(u, v)] < v.key:
                 v.parent = u
                 Q.decrease_key(v.key, self.weight[(u, v)])
示例#3
0
def mergeKListsMinHeap(lists):
    """
    Solution with MinHeap
    :type lists: List[ListNode]
    :rtype: ListNode
    """
    mh = MinHeap()
    for node in lists:
        if node:
            mh.insert(node)

    head = ListNode(0)
    node = head
    while mh.size > 0:
        minNode = mh.delMin()
        nextNode = minNode.next
        if nextNode:
            mh.insert(nextNode)
        node.next = minNode
        node = node.next

    return head.next
def sift_down_test_4():
    heap = MinHeap([70, 20, 25, 22, 30, 25, 28], already_heap=True)
    print(f"Original: {heap}")
    heap.sift_down(0)
    print(heap)
def decrease_key_test_3():
    heap = MinHeap([10, 20, 25, 22, 30, 25, 28], already_heap=True)
    heap.decrease_key(0, 11)  # Supposed to throw an error
    print(heap)
def insert_test_1():
    heap = MinHeap()
    for i in [60, 20, 10, 70, 35, 24]:
        heap.insert(i)
        print(heap)
def decrease_key_test_1():
    heap = MinHeap([10, 20, 25, 22, 30, 25, 28], already_heap=True)
    for i in range(7):
        heap.decrease_key(i, heap.arr[i] - 1)
    print(heap)
def decrease_key_test_2():
    heap = MinHeap([10, 20, 25, 22, 30, 25, 28], already_heap=True)
    heap.decrease_key(6, 0)
    print(heap)
from sys import stdin

from BinaryHeap import MinHeap

t = int(stdin.readline())
heap = MinHeap()
for _ in range(t):

    inp = list(map(int, stdin.readline().strip().split(" ")))
    operation = inp[0]
    element = inp[1] if len(inp) == 2 else 0

    if operation == 1:
        heap.insert_element(element)
    elif operation == 2:
        heap.delete_element(element)
    elif operation == 3:
        print(heap.peek())
def build_heap_test_1():
    heap = MinHeap([60, 20, 10, 70, 35, 24])
    print(heap)
def build_heap_test_2():
    heap = MinHeap()
    heap.build_heap([10, 20, 30, 40, 50, 40, 30, 20, 10])
    print(heap)
    def setUp(self):
        self.intHeap1 = MinHeap(dtype=int)
        self.intHeap2 = MinHeap(dtype=int)

        self.intHeap1.insert(5).insert(4).insert(3).insert(2).insert(1)
        self.intHeap2.insert(1).insert(2).insert(3).insert(4).insert(5)
def sift_down_test_3():
    heap = MinHeap([10, 20, 25, 12, 30, 25, 28], already_heap=True)
    print(f"Original: {heap}")
    heap.sift_down(1)
    print(heap)
def sift_down_test_1():
    heap = MinHeap([10, 20, 25, 22, 30, 25, 28], already_heap=True)
    print(f"Original: {heap}")
    for i in range(len(heap)):
        heap.sift_down(i)
        print(heap)  # No changes should occur.
def sift_up_test_4():
    heap = MinHeap([10, 20, 25, 18, 19, 25, 28], already_heap=True)
    print(f"Original: {heap}")
    heap.sift_up(4)
    heap.sift_up(3)
    print(heap)
def sift_up_test_3():
    heap = MinHeap([10, 20, 25, 22, 30, 25, 9], already_heap=True)
    print(f"Original: {heap}")
    heap.sift_up(6)
    print(heap)
def sift_up_test_1():
    heap = MinHeap([10, 20, 25, 22, 30, 25, 28], already_heap=True)
    print(f"Original: {heap}")
    for i in range(7):
        heap.sift_up(i)
        print(heap)  # No change expected
class TestMinHeap(TestCase):

    def setUp(self):
        self.intHeap1 = MinHeap(dtype=int)
        self.intHeap2 = MinHeap(dtype=int)

        self.intHeap1.insert(5).insert(4).insert(3).insert(2).insert(1)
        self.intHeap2.insert(1).insert(2).insert(3).insert(4).insert(5)

    def test_heapSize(self):
        self.assertEqual(self.intHeap1.getSize(), 5)
        self.assertEqual(self.intHeap2.getSize(), 5)

    def test_getMinimumKey(self):
        self.assertEqual(self.intHeap1.getMinKey(), 1)
        self.assertEqual(self.intHeap1.getMinKey(), 1)

    def test_deleteMinKey(self):
        self.assertEqual(self.intHeap1.delMinKey(), 1)
        self.assertEqual(self.intHeap1.delMinKey(), 2)
        self.assertEqual(self.intHeap1.delMinKey(), 3)
        self.assertEqual(self.intHeap1.delMinKey(), 4)
        self.assertEqual(self.intHeap1.delMinKey(), 5)
        self.assertEqual(self.intHeap1.delMinKey(), None)

        self.assertEqual(self.intHeap2.delMinKey(), 1)
        self.assertEqual(self.intHeap2.delMinKey(), 2)
        self.assertEqual(self.intHeap2.delMinKey(), 3)
        self.assertEqual(self.intHeap2.delMinKey(), 4)
        self.assertEqual(self.intHeap2.delMinKey(), 5)
        self.assertEqual(self.intHeap2.delMinKey(), None)

    def tearDown(self):
        del self.intHeap1
        del self.intHeap2
def extract_min_test_1():
    heap = MinHeap([10, 20, 25, 22, 30, 25, 28], already_heap=True)
    print(f"Original: {heap}")
    for i in range(1, 8):
        print(heap.extract_min(), heap)
def extract_min_test_2():
    heap = MinHeap([90, 90, 90], already_heap=True)
    print(f"Original: {heap}")
    for i in range(3):
        print(heap.extract_min(), heap)
def extract_min_test_3():
    heap = MinHeap([], already_heap=True)
    for i in range(3):
        print(heap.extract_min(), heap)
from BinaryHeap import MinHeap
from sys import stdin

n = int(stdin.readline())
tasks = []
total_waiting_time = 0
totalTime = 0
cookingPriority = MinHeap()
for _ in range(n):
    A, C = map(int, stdin.readline().strip().split(" "))
    tasks.append((A, C))

tasks.sort()

index = 0

while tasks or cookingPriority.currentSize > 0:
    while tasks and tasks[index][0] <= totalTime:
        cookingPriority.insert_element(tasks.pop(0)[::-1])

    if cookingPriority.currentSize > 0:
        current_task = cookingPriority.peek()
        totalTime += current_task[0]
        waitingTime = totalTime - current_task[1]
        total_waiting_time += waitingTime
        cookingPriority.delete_min()
    else:
        cookingPriority.insert_element(tasks.pop(0)[::-1])
        current_task = cookingPriority.peek()
        totalTime = current_task[1]
def insert_test_2():
    heap = MinHeap([10, 20, 30, 40, 50])
    for i in [40, 30, 20, 10]:
        heap.insert(i)
        print(heap)