Jesse and Cookies

  • + 0 comments

    Python:

    def cookies(k: int, A: list) -> int:
        heapq.heapify(A)
        counter = 0         # no. of operations
        while A[0] < k:     # smallest element always first
            if len(A) < 2:
                return -1
            first = heapq.heappop(A) 
            second = heapq.heappop(A) 
            heapq.heappush(A, first + 2 * second)
            counter += 1
        return counter