• + 1 comment

    Here is my solution. It uses python heapq library. It is fast

    def cookies(k, A):
        import heapq
    
        heapq.heapify(A)
        count = 0
        while not all(i >= k for i in A): # until all cookies are >= k
            if len(A) == 1:
                return -1
            a, b = heapq.heappop(A), heapq.heappop(A)
            c = a + 2 * b
            heapq.heappush(A, c)
            count += 1
        return count
    
    • + 0 comments

      do you knwo what is the difference between your code and: for some reason having if A[0] >= k: inside while loop makes case 18 fail def cookies(k, A): # Write your code here heapq.heapify(A) count = 0 while len(A) >= 2: if A[0] >= k: return count x = heapq.heappop(A) y = heapq.heappop(A) heapq.heappush(A, x+(2*y)) count += 1 return -1