• + 0 comments

    JAVA Using PriorityQueue

    class Result {
        public static int cookies(int k, List<Integer> A) {
            if (A.size() == 0) return -1;
            if (A.size() == 1) {
                return (A.get(0) < k) ? -1 : 0;
            }
            Queue<Integer> heap = new PriorityQueue<>(A);
            int steps = 0;
            while(heap.peek() < k){
                if(heap.size() < 2) return -1;
                int c1 = heap.poll();
                int c2 = heap.poll();
                heap.add(c1 + (c2 * 2));
                steps += 1;
            }
            return steps;
        }
    }