Jesse and Cookies

  • + 0 comments

    simple python solution

    def cookies(k, A):
        heapq.heapify(A)
        count = 0
        while True:
            c1 = heapq.heappop(A)
            if c1 >= k:
                return count
            if len(A) == 0:
                return -1
            c2 = heapq.heappop(A)
            new_cookie = c1 + 2 * c2
            heapq.heappush(A, new_cookie)
            count += 1
        return count