• + 0 comments

    Thanks ! Switching to Java 7 worked for me too.

        static int cookies(int k, int[] A) {
            PriorityQueue<Integer> pq = new PriorityQueue<>(A.length);
            for (int val : A) {
                pq.add(val);
            }
            int counter = 0;
            while (pq.size() > 1 && pq.peek() < k) {
                int smallest = pq.poll();
                int second = pq.poll();
                pq.add(smallest + 2*second);
                counter++;
            }
            
            if (pq.size() > 1 || pq.peek() >= k) {
                return counter;
            } else {
                return -1;
            }
        }