We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
publicstaticintcookies(intk,List<Integer>A){PriorityQueue<Integer>pq=newPriorityQueue<Integer>(A);intiterations=0;while(pq.size()>0){Integerlowest=pq.poll();if(lowest>=k){returniterations;}IntegersecondLowest=pq.poll();if(secondLowest!=null){finalIntegercombined=lowest+(2*secondLowest);pq.offer(combined);}else{return-1;//this will only happen if 1 item left less than k}iterations++;}returniterations;//would only get here is pq size of 0}
Cookie support is required to access HackerRank
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 →