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.
100% Javascript solution that should be easy to understand
functionminimumPasses(m,w,p,n){if(n<=p){returnMath.ceil(n/(m*w));}letcurrentDay=0;letcandy=0;while(candy<n){constremainingDaysWithCurrentProduction=Math.ceil((n-candy)/(m*w));constcanBuy=Math.floor(candy/p);if(canBuy===0){// keep saving until enough for a purchase consti=Math.ceil((p-candy)/(m*w));currentDay+=i;candy+=(m*w*i);continue;}consttotal=m+w+canBuy;consthalf=Math.floor(total/2);letmNew=m;letwNew=w;if(m>w){mNew=Math.max(m,half);wNew=total-mNew;}else{wNew=Math.max(w,half);mNew=total-wNew;}constrest=candy%p;constremainingDaysWithIncreasedProduction=Math.ceil((n-rest)/(mNew*wNew));if(remainingDaysWithCurrentProduction<remainingDaysWithIncreasedProduction){// stop purchasing and produce until donereturncurrentDay+remainingDaysWithCurrentProduction;}// purchases machines and workers and continuecandy=rest;m=mNew;w=wNew;currentDay+=1;candy+=(m*w);}returncurrentDay;}
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 →
100% Javascript solution that should be easy to understand