You are viewing a single comment's thread. Return to all 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; } }
Seems like cookies are disabled on this browser, please enable them to open this website
I agree to HackerRank's Terms of Service and Privacy Policy.
Jesse and Cookies
You are viewing a single comment's thread. Return to all comments →
JAVA Using PriorityQueue