Jesse and Cookies

  • + 0 comments

    Simple Python solution using a Heap:

        n = len(A)
        iterations=0
        i=0
        heapq.heapify(A)
        while i < n:
            minA = heapq.heappop(A) 
            if minA < k:
                if len(A)<1:
                    return -1
                minB = heapq.heappop(A) 
                newValue= (1 * minA) + (2 * minB)
                heapq.heappush(A,newValue)
                i+=1
                iterations+=1
            else:
                break
        return iterations