Jesse and Cookies

Sort by

recency

|

12 Discussions

|

  • + 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
    
  • + 0 comments

    C# code

    public static int cookies(int k, List<int> A)
        {
            
            if (A.Count < 1) 
                return -1;
                
            int count = 0;
            PriorityQueue<int, int> q = new PriorityQueue<int, int>();
            
            for(int i =0; i < A.Count; i++)
            {
                q.Enqueue(A[i], A[i]);
            }
            
            while(q.Count > 1 && q.Peek() < k)
            {
                int min = q.Peek();
                int val = q.Dequeue()  + 2 * q.Dequeue() ;
                if (val < min) 
                  return -1;
                count++;
                q.Enqueue(val,val);
                
            }
            
            if (q.Peek() >= k) 
                return count;
            else 
                return -1;
            
    
        }