Jesse and Cookies

  • + 0 comments

    java

            if (A == null || A.isEmpty()) {
                return -1;
            }
            int count = 0;
            PriorityQueue<Integer> pq = new PriorityQueue<>(A);
            while (0 < pq.size()) {
                if (k <= pq.peek()) {
                    return count;
                }
                if (pq.size() == 1) {
                    return -1;
                }
                pq.offer(pq.poll() + 2 * pq.poll());
                count++;            
            }
    
            return -1;
        }