You are viewing a single comment's thread. Return to all comments →
Java O(n log n)
public static int cookies(int k, List<Integer> A) { PriorityQueue<Integer> pq = new PriorityQueue<>(A); int operations = 0; while (pq.size() > 1 && pq.peek() < k) { int first = pq.poll(); int second = pq.poll(); int combinedSweetness = first + (2 * second); pq.offer(combinedSweetness); operations++; } return pq.peek() >= k ? operations : -1; }
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 →
Java O(n log n)