Jesse and Cookies

Sort by

recency

|

13 Discussions

|

  • + 0 comments

    Simple Python solution using a Heap:

        n = len(A)
        iterations=0
        i=0
        heapq.heapify(A)
        while i < n:
            minA = heapq.heappop(A) 
            if minA < k:
                if len(A)<1:
                    return -1
                minB = heapq.heappop(A) 
                newValue= (1 * minA) + (2 * minB)
                heapq.heappush(A,newValue)
                i+=1
                iterations+=1
            else:
                break
        return iterations
    
  • + 0 comments

    Python, using stack for premade cookies, queue for new cookies

    def getsmallest(stack, queue):
        if len(stack) == 0:
            return queue.popleft()
        elif len(queue) == 0:
            return stack.pop()
        return queue.popleft() if queue[0] < stack[-1] else stack.pop()
        
    def cookies(k, startstack):
        startstack.sort(reverse=True)
        createdqueue = deque()
        numops = 0
        while True:
            if (len(startstack) == 0 or startstack[-1] >= k) and (len(createdqueue) == 0 or createdqueue[0] >= k):
                return numops
            if (len(startstack) + len(createdqueue) < 2):
                return -1
                
            numops += 1
            s1 = getsmallest(startstack, createdqueue)
            s2 = getsmallest(startstack, createdqueue)
            createdqueue.append(s1 + 2 * s2)
    
  • + 0 comments

    Java O(n log n)

    public static int cookies(int k, List<Integer> A) {
            PriorityQueue<Integer> pq = new PriorityQueue<>(A);
            int operations = 0;
    
            while (pq.size() > 1 && pq.peek() < k) {
                int first = pq.poll();
                int second = pq.poll();
                int combinedSweetness = first + (2 * second);
                pq.offer(combinedSweetness);
                operations++;
            }
    
            return pq.peek() >= k ? operations : -1;
        }
    
  • + 0 comments

    Java 15 solution using PriorityQueue:

    public static int cookies(int k, List<Integer> cookies){
        PriorityQueue<Integer> pq = new PriorityQueue<Integer>(cookies);
        if(isAllCookiesSweeterThanK(k,pq))return 0;
    
        int counter = 0;
        while(!isAllCookiesSweeterThanK(k, pq)){
            if(pq.size()==1)return -1;
            pq.add(pq.poll() + pq.poll()*2);
            counter++;
        }
    
        return counter;
    }
    
    private static boolean isAllCookiesSweeterThanK(int k, PriorityQueue<Integer> pq){
        Optional<Integer> ans = pq.stream().filter(i -> i < k).findFirst();
        if(ans.isEmpty())return true;
        return false;
    }
    
  • + 0 comments
    import heapq
    def cookies(k, A):
        operations = 0
        heapq.heapify(A) 
    
        while A[0] < k and len(A) > 1:
            heapq.heappush(A, heapq.heappop(A) + 2 * heapq.heappop(A))
            operations += 1
    
        return operations if A[0] >= k else -1