0
import heapq
def getMaxUnit(num,boxes,UnitSize,UnitSize,unitPerBox, truckSize):

    if truckSize == 0 or num == 0:
        return 0

    h = []
    for i in range(num):
        h.append((-1*unitPerBox[i],boxes[i]))
    heapq.heapify(h)
    maxCapacity = 0
    while truckSize>=0 and len(h) != 0:
        popped = heapq.heappop(h)
        truckSize = truckSize-popped[1]
        available = popped[1]
        if truckSize < 0:
            available = popped[1]+truckSize
        maxCapacity = maxCapacity + available*(-1*popped[0])
    return maxCapacity


I'm trying to find the time complexity of the code here. I'm confused with what the time complexity of heapq.heappop here as it needs to maintain heap property every time we pop an element.

tadman
  • 208,517
  • 23
  • 234
  • 262

0 Answers0