We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
  • HackerRank Home

    HackerRank

  • |
  • Prepare
  • Certify
  • Compete
  • Hiring developers?
  1. Prepare
  2. Data Structures
  3. Heap
  4. Jesse and Cookies
  5. Discussions

Jesse and Cookies

Problem
Submissions
Leaderboard
Discussions
Editorial

Sort 326 Discussions, By:

recency

Please Login in order to post a comment

  • Stelios10
    3 days ago+ 0 comments

    Short JAVA solution, using priority-queue to optimize sorted insertions

        public static int cookies(int k, List<Integer> cookies) {
            int result = 0;
            PriorityQueue<Integer> cookiesSorted = new PriorityQueue<>(cookies);
            
            while (cookiesSorted.size() >= 2 && cookiesSorted.peek() < k) {
                cookiesSorted.add(cookiesSorted.poll() + 2 * cookiesSorted.poll());
                result++;
            }
            
            return cookiesSorted.peek() < k ? -1 : result;
        }
    
    0|
    Permalink
  • srmckenna
    2 months ago+ 0 comments
        public static int cookies(int k, List<Integer> A) {
            PriorityQueue<Integer> pq = new PriorityQueue<Integer>(A);
            int iterations = 0;
            
            while (pq.size() > 0) {
                Integer lowest = pq.poll();
                if (lowest >= k) {
                    return iterations;
                }
                Integer secondLowest = pq.poll();
                if (secondLowest != null) {
                    final Integer combined = lowest + (2 * secondLowest);
                    pq.offer(combined);
                } else {
                    return -1; //this will only happen if 1 item left less than k
                }
                
                iterations++;
            }
            
            return iterations; //would only get here is pq size of 0
        }
    
    0|
    Permalink
  • Super3laa
    3 months ago+ 0 comments
    int cookies(int k, vector<int> A) {
        priority_queue<int, vector<int>, greater<int> > pq;
        for(int i : A)pq.push(i);
        int ans = 0;
        int temp = pq.top();
        pq.pop();
        while(temp < k){
            if(pq.empty()) return -1;
            int secTemp = pq.top();
            pq.pop();
            pq.push(temp+2*secTemp);
            temp = pq.top();
            pq.pop();
            ans++;
        }
        return ans ;
        
    }``
    
    0|
    Permalink
  • pandit_manish
    3 months ago+ 0 comments
    public static int cookies(int k, List<Integer> A) {
            if(A.size()<=1) return -1;
            A = A.stream().filter(x -> x < k).collect(Collectors.toList());
            if(A.size()==0) return 0; //no operations needed
            if(A.size()==1) return 1;
            PriorityQueue<Integer> p = new PriorityQueue<>();
            p.addAll(A);
            int operations = 0;
            while(p.peek() < k && p.size() >=2){
                int smallest = p.remove();
                int secondSmallest = p.remove();
                p.add(smallest + 2*secondSmallest);
                ++operations;
           }
           if(p.peek()>=k) return operations;
           else return -1;
        }
    
    0|
    Permalink
  • lokesh_001
    3 months ago+ 1 comment

    Can anyone tell what's wrong in this

    def cookies(k, A):
        # Write your code here
        count = 0
        A.sort()
        
        while(A[0]<k):
            if(len(A)==1):
                return -1
            A.remove(A[0])
            A.remove(A[1])
            A.append(A[0]+2*A[1])
            count+=1
            A.sort()
            
        return count
    
    0|
    Permalink
Load more conversations

Need Help?


View editorial
View top submissions
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy