Jesse and Cookies

Sort by

recency

|

34 Discussions

|

  • + 0 comments

    here is hackerrank jesse and cookies problem solution in Python, java, c++, c and javascript

  • + 0 comments

    In JAVA8 :

    public static int cookies(int k, List<Integer> A) {
            // Write your code here
            PriorityQueue <Integer> pq = new PriorityQueue<>(A);        
            int count = 0;
            while (pq.size() > 1 && pq.peek()<k) {
                pq.offer(pq.poll() + 2 * pq.poll());
                count++;
            }
            return  pq.peek() >= k ? count : -1;
        }
    
  • + 0 comments

    C#:

    public static int cookies(int k, List<int> A)
        {
            var  items = new PriorityQueue<int, int>();
    
            for (int i = 0; i < A.Count; i++)
            {   
                items.Enqueue(A[i], A[i]);
            }
    
            int count = 0;
            while (items.Count > 1 && items.Peek() < k){
                var newItem = items.Dequeue() + (2 * items.Dequeue());
                
                items.Enqueue(newItem, newItem);
    
                count++;
            }
    
            return items.Peek() < k ? -1 : count;
        }
    
  • + 0 comments

    short java8

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

    Min Heap solves this under 10 lines. (C++ / Priority Queue).