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.
I got all the testcases passed when I used a max heap
intcookies(intk,vector<int>&A){intoperations=0;// Create a max heap (priority queue) with negative valuespriority_queue<int>p;for(intcookie:A){p.push(-cookie);}// Perform operations until the minimum element is greater than or equal to kwhile(-p.top()<k&&p.size()>1){intcookie1=p.top();p.pop();intcookie2=p.top();p.pop();intnewCookie=cookie1+2*cookie2;p.push(newCookie);operations++;}// Check if the minimum element is greater than or equal to kif(-p.top()<k)return-1;// Not possible to achieve kelsereturnoperations;// Return the number of 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 →
I got all the testcases passed when I used a max heap