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.
longminimumPasses(longm,longw,longp,longn){if(m>w)swap(m,w);longbest_days=LONG_MAX;longcandy=0;longday=0;while(true){// days to reach the goal without additional resourcesconstautodays_to_goal=1+(n-candy-1)/m/w;// prevent m*w overflow // This utilizes the fact that local optimum is also global optimum// i.e., as we increase # of days to buy resources, best_days will first decrease and then increase if(best_days<day+days_to_goal)break;best_days=day+days_to_goal;// days to buy next resourceconstautodays_to_buy=1+(p-candy-1)/m/w;// prevent m*w overflowday+=days_to_buy;candy+=days_to_buy*m*w;// buy resourceconstautomw=m+w+candy/p;candy%=p;w=max(w,1+(mw-1)/2);m=mw-w;}returnbest_days;}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Making Candies
You are viewing a single comment's thread. Return to all comments →
C++ solution