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.
frommathimportceildefminimumPasses(m,w,p,n):min_turns=ceil(n/(m*w))#minturnswithouteverbuyingmoreworkersormachinesifp>n:returnmin_turnsturn=0candies=0whilecandies<n:# skip to next turn with buying opportunityskip_turns=max([ceil((p-candies)/(m*w)),1])turn+=skip_turnsifturn>min_turns:# no reason to continue buying the best strategy is to just save from last min_turn calcreturnmin_turnscandies+=m*w*skip_turnsifcandies>=p:# get new machinesnew_machines=candies// pcandies-=new_machines*pdiff=m-w# try to balance workers and machinesifdiff>0:w_buy=min(new_machines,diff)w+=w_buynew_machines-=w_buyelse:m_buy=min(new_machines,abs(diff))m+=m_buynew_machines-=m_buy# split remainder after balancing and buy half workers and half machinesw_buy=new_machines// 2w+=w_buym+=new_machines-w_buy# check how long till goal without buying anythingmin_turns=min(ceil((n-candies)/(m*w))+turn,min_turns)returnturn
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 →
Time Complexity O(log n) Space Complexity O(1)