You are viewing a single comment's thread. Return to all comments →
Java solution using priority queue:
public static int cookies(int k, List<Integer> A) { Queue<Integer> queue = new PriorityQueue<>(); for (Integer num: A) { queue.offer(num); } int least1; int least2; int op = 0; while (queue.size() >= 2 && queue.peek() < k) { least1 = queue.poll(); least2 = queue.poll(); queue.offer(least1 + 2 * least2); op++; } if (queue.peek() >= k) { return op; } return -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 solution using priority queue: