I have been working on DSA problems and have also been trying to calculate the time and space complexity of my solutions. I feel like I can do ok with most solutions, however there are many, like the one below, that stump me.
while paymentTotal <= k:
temp = heapq.heappop(heap)
if paymentTotal + temp > k:
break
paymentTotal += temp
toyCount += 1
In this case the loop seems to be able to be broken down into this (heap is a minHeap that contains random ints. k is also a random int):
k = n
p = 0
while p <= k:
t = heap.heapqpop(heap)
p += t
So this would mean that the loop runs while p+t <= k, but because t does not increment p in a consistent manner and k is random I am having a hard time conceptualizing what this time complexity would be. The amount of times the loop runs obviously changes based on what k is and also what ints are popped from the heap, but I am lost on how to express that mathematically and in Big O.
heappopitself is O(lg n), not O(1), in the worst case.