Jesse and Cookies

  • + 0 comments

    In JAVA8 :

    public static int cookies(int k, List<Integer> A) {
            // Write your code here
            PriorityQueue <Integer> pq = new PriorityQueue<>(A);        
            int count = 0;
            while (pq.size() > 1 && pq.peek()<k) {
                pq.offer(pq.poll() + 2 * pq.poll());
                count++;
            }
            return  pq.peek() >= k ? count : -1;
        }