Jesse and Cookies

  • + 0 comments
    import heapq
    def cookies(k, A):
        operations = 0
        heapq.heapify(A) 
    
        while A[0] < k and len(A) > 1:
            heapq.heappush(A, heapq.heappop(A) + 2 * heapq.heappop(A))
            operations += 1
    
        return operations if A[0] >= k else -1