Jesse and Cookies

Sort by

recency

|

186 Discussions

|

  • + 0 comments

    who's coding in C# - just a friendly recommendation: use PriorityQueue here..

  • + 0 comments

    This description is wrong. The step indicating you remove 7 and 8 is not possible as 7 was removed in the previous iteration when 6 and 7 were the lowest numbers.

    Also why does nearly every single comment error with:

    yUo fAiLeD tHe tUrNiNg TeSt

    I... I don't know if I'm a human anymore.

  • + 0 comments

    java

            if (A == null || A.isEmpty()) {
                return -1;
            }
            int count = 0;
            PriorityQueue<Integer> pq = new PriorityQueue<>(A);
            while (0 < pq.size()) {
                if (k <= pq.peek()) {
                    return count;
                }
                if (pq.size() == 1) {
                    return -1;
                }
                pq.offer(pq.poll() + 2 * pq.poll());
                count++;            
            }
    
            return -1;
        }
    
  • + 1 comment
    import heapq
    def cookies(k, A):
        heapq.heapify(A)
        ways =0
        
        while len(A) > 1 and A[0] < k :
            a = heapq.heappop(A) 
            b = heapq.heappop(A) *2
            heapq.heappush(A, (a+b))
            ways +=1
    
    				
            
        if A[0] < k:
            return -1
        return ways
    
  • + 0 comments

    Python Solution with using Heap directly,

    def getElement(arr, arr2, i, j): if (len(arr) > i and len(arr2) > j): if (arr[i] <= arr2[j]): return arr[i], i+1, j else: return arr2[j], i, j+1 elif (len(arr) > i): return arr[i], i+1, j elif (len(arr2) > j): return arr2[j], i, j+1 return 0, i, j

    def cookies(k, A): arr = sorted(A) arr2 = [] i, j, ln = 0, 0, len(arr)

    if (ln == 0 and k > 0):
        return -1
    count = 0
    
    if (ln > 0):
        while((i + j + 1 < len(arr) + len(arr2)) and ((len(arr) > i and arr[i] < k) or (len(arr2) > j and arr2[j] < k))):
            count += 1
            firstSmallest, i, j = getElement(arr, arr2, i, j)
            secondSmallest, i, j = getElement(arr, arr2, i, j)
            newCookie = firstSmallest + (2 * secondSmallest)
            arr2.append(newCookie)
        if ((len(arr) > i and arr[i] < k) or (len(arr2) > j and arr2[j] < k)):
            count = -1
    return count