You are viewing a single comment's thread. Return to all comments →
Piece of cake. Using min heap implemented by PriorityQueue Java 8
public static int cookies(int k, List<Integer> A) { int operations = 0; PriorityQueue<Integer> cookies = new PriorityQueue<>(A); while(cookies.size() > 1 && cookies.peek() < k) { int combined = cookies.poll() + 2 * cookies.poll(); cookies.offer(combined); operations++; } return cookies.peek() < k ? -1 : operations; }
Seems like cookies are disabled on this browser, please enable them to open this website
Jesse and Cookies
You are viewing a single comment's thread. Return to all comments →
Piece of cake. Using min heap implemented by PriorityQueue Java 8