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.
My Java solution using a Priority Queue or min heap
publicstaticintcookies(intk,List<Integer>A){// Write your code herePriorityQueue<Integer>pq=newPriorityQueue<>();intoperations=0;for(inta:A){if(a<k){pq.add(a);}}if(pq.isEmpty()){return0;}while(!pq.isEmpty()){inta=pq.remove();if(!pq.isEmpty()){intb=pq.remove();intres=a+2*b;if(res<k){pq.add(res);}}operations++;}returnoperations==A.size()?-1:operations;}
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 →
My Java solution using a Priority Queue or min heap