• + 0 comments

    Python solution with heapq

    import heapq
    
    def cookies(k, A):
        heapq.heapify(A)
        minVal = heapq.heappop(A)
        if minVal >= k: return 0
        
        heapq.heappush(A, minVal)
        count = 0
        while(len(A) >= 2):
            min1 = heapq.heappop(A)
            min2 = heapq.heappop(A)
            heapq.heappush(A, min1 + min2 * 2)
            count += 1
            minNew = heapq.heappop(A)
            if minNew >= k: return count
            heapq.heappush(A, minNew)
        return -1