Jesse and Cookies

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