You are viewing a single comment's thread. Return to all comments →
public static int cookies(int k, List<Integer> A) { if(A.size()<=1) return -1; A = A.stream().filter(x -> x < k).collect(Collectors.toList()); if(A.size()==0) return 0; //no operations needed if(A.size()==1) return 1; PriorityQueue<Integer> p = new PriorityQueue<>(); p.addAll(A); int operations = 0; while(p.peek() < k && p.size() >=2){ int smallest = p.remove(); int secondSmallest = p.remove(); p.add(smallest + 2*secondSmallest); ++operations; } if(p.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 →