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 with o(n log n) time complexity and o(1) space complexity:
publicstaticlongmarcsCakewalk(List<Integer>calorie){// Sort the calorie array in descending orderCollections.sort(calorie,Collections.reverseOrder());// Calculate the total miles to walk using powers of 2 (bit-shifting)longtotalMiles=0;for(inti=0;i<calorie.size();i++){longcurrentMilesToWalk=(1L<<i)*calorie.get(i);// More efficient power of 2 calculationtotalMiles+=currentMilesToWalk;}returntotalMiles;}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Marc's Cakewalk
You are viewing a single comment's thread. Return to all comments →
My Java solution with o(n log n) time complexity and o(1) space complexity: