You are viewing a single comment's thread. Return to all comments →
my java solution:
static int cookies(int k, int[] A) { PriorityQueue<Integer> pq = new PriorityQueue<>(); int operations = 0; //Initialize priority queue for(int a : A) { pq.add(a); } while(pq.size() > 1) { if(pq.peek() >= k) { return operations; } else { int cookie1 = pq.poll(); int cookie2 = pq.poll(); int newCookie = cookie1 + cookie2 * 2; pq.add(newCookie); operations++; } } if(pq.size() > 0 && pq.peek() >= k) { return operations; } else { 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 →
my java solution: