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.
We can try all possible round numbers and see if we can make candies in each required number of rounds, however, we notice that if we can produce candies in x rounds then any number of rounds greater than x is redundant to check. SO we can try a round and if we can make the required number of candies we can try for a smaller number of rounds, if we cant then we should try for a higher number of rounds ... this is a binary search! Don’t worry if you didn’t figure that out, recognising this comes with practise.
The check function is a bit complicated since you have to simulate producing candies and trying out purchasing more workers but this can be done if we always check that the current workers and machines setup can reach the required candies before trying to buy an additional worker or machine (which can be done optimally by trying to keep them the same), after which we check again if that setup produces enough for the solution within the rounds and so on. IF we can’t purchase new workers/machines then we have to wait a certain number of rounds (while still producing candies) so we keep checking if we’re reached the target number of candies but if we run out of rounds at any point we return False.
By carefully implementing the check function, this question becomes fairly straightforward! spend time understanding the check function for yourself and what each section is doing to solidify your understanding.
Implementation
defcheckIfMidPassesPossible(machines,workers,price,target,rounds):# Immediately return if we can produce enough in one roundifmachines*workers>=target:returnTrue# We maintaing the candies we currently have and the rounds we have leftcurrent_candies=machines*workersrounds-=1# We go till we have enough rounds to do produce (and potentially purchase in)whilerounds>0:# STEP 1) Check if we can reach the end w/ the current production rateremaining_candies=target-current_candiesneeded_rounds=(remaining_candies)//(machines*workers)ifremaining_candies%(machines*workers)>0:needed_rounds+=1# To round up for day with partial productionifneeded_rounds<=rounds:returnTrue# STEP 2) Wait until we have enough to purchase a worker and return False# if we need to wait too longifcurrent_candies<price:additional_needed=price-current_candiesrounds_needed_to_buy=additional_needed//(machines*workers)ifadditional_needed%(machines*workers)>0:rounds_needed_to_buy+=1rounds-=rounds_needed_to_buy# MAINTAIN ROUNDS LEFTifrounds<=0:returnFalsecurrent_candies+=rounds_needed_to_buy*machines*workers# MAINTAIN CANDIES PRODUCED# STEP 3) Make the optimal purchase of worker or machinecurrent_candies-=price# MAINTAIN CANDIES PRODUCEDmachines,workers=(machines+1,workers)ifmachines<=workerselse(machines,workers+1)# If we run out of rounds, we haven't reached the target and there aren't any more roundreturnFalsedefminimumPasses(m,w,p,n):low,high=1,10**12# Set a large upper bound, we set low to 1 since we never start# with any candieswhilelow<high:mid=(low+high)//2ifcheckIfMidPassesPossible(m,w,p,n,mid):high=mid# Look for fewer passeselse:low=mid+1# Increase passesreturnlow
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 →
Intuition & Approach:
We can try all possible round numbers and see if we can make candies in each required number of rounds, however, we notice that if we can produce candies in x rounds then any number of rounds greater than x is redundant to check. SO we can try a round and if we can make the required number of candies we can try for a smaller number of rounds, if we cant then we should try for a higher number of rounds ... this is a binary search! Don’t worry if you didn’t figure that out, recognising this comes with practise.
The check function is a bit complicated since you have to simulate producing candies and trying out purchasing more workers but this can be done if we always check that the current workers and machines setup can reach the required candies before trying to buy an additional worker or machine (which can be done optimally by trying to keep them the same), after which we check again if that setup produces enough for the solution within the rounds and so on. IF we can’t purchase new workers/machines then we have to wait a certain number of rounds (while still producing candies) so we keep checking if we’re reached the target number of candies but if we run out of rounds at any point we return False.
By carefully implementing the check function, this question becomes fairly straightforward! spend time understanding the check function for yourself and what each section is doing to solidify your understanding.
Implementation