You are viewing a single comment's thread. Return to all comments →
Java 15 solution using PriorityQueue:
public static int cookies(int k, List<Integer> cookies){ PriorityQueue<Integer> pq = new PriorityQueue<Integer>(cookies); if(isAllCookiesSweeterThanK(k,pq))return 0; int counter = 0; while(!isAllCookiesSweeterThanK(k, pq)){ if(pq.size()==1)return -1; pq.add(pq.poll() + pq.poll()*2); counter++; } return counter; } private static boolean isAllCookiesSweeterThanK(int k, PriorityQueue<Integer> pq){ Optional<Integer> ans = pq.stream().filter(i -> i < k).findFirst(); if(ans.isEmpty())return true; return false; }
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 15 solution using PriorityQueue: