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
  • Prepare
    NEW
  • Certify
  • Compete
  • Career Fair
  • Hiring developers?
  1. Prepare
  2. Data Structures
  3. Heap
  4. Jesse and Cookies
  5. Discussions

Jesse and Cookies

Problem
Submissions
Leaderboard
Discussions
Editorial

    You are viewing a single comment's thread. Return to all comments →

  • mr_cleverpig
    3 months ago+ 0 comments

    Simple Java solution using PriorityQueue

    public static int cookies(int k, List<Integer> A) {
        // Write your code here
            PriorityQueue<Integer> q = new PriorityQueue<>();
            
            for (int i : A) q.add(i);
            
            int operations = 0;
            
            while (q.peek() < k && q.size() >= 2){
                int smaller = q.remove();
                int small = q.remove();
                int mix = smaller + (small * 2);
                q.add(mix);
                operations++;
            }
            
            return (q.peek() >= k) ? operations : -1; 
        }
    
    0|
    Permalink
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy