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


我试图在这里找到代码的时间复杂度。我对 heapq.heappop 的时间复杂度感到困惑,因为每次弹出元素时它都需要维护堆属性。

4

0 回答 0