• + 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; 
        }