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.
#!/bin/python3importmathimportosimportrandomimportreimportsys## Complete the 'minimumPasses' function below.## The function is expected to return a LONG_INTEGER.# The function accepts following parameters:# 1. LONG_INTEGER m# 2. LONG_INTEGER w# 3. LONG_INTEGER p# 4. LONG_INTEGER n#defcheckIfMidPassesPossible(m,w,p,n,rounds):candies=0whilerounds>0:ifm*w>=n:returnTrueremaining_candies=n-candiesneeded_rounds=math.ceil(remaining_candies/(m*w))ifneeded_rounds<=rounds:returnTrueifcandies<p:rounds_needed_to_buy=math.ceil((p-candies)/(m*w))ifrounds_needed_to_buy>=rounds:returnFalserounds-=rounds_needed_to_buycandies+=rounds_needed_to_buy*m*wcandies-=pifm<=w:m+=1else:w+=1returnFalsedefminimumPasses(m,w,p,n):#Write your code hereifp>n:returnmath.ceil(n/(m*w))low,high=1,10**12whilelow<high:mid=(low+high)// 2ifcheckIfMidPassesPossible(m,w,p,n,mid):high=midelse:low=mid+1returnlowif__name__=='__main__':fptr=open(os.environ['OUTPUT_PATH'],'w')first_multiple_input=input().rstrip().split()m=int(first_multiple_input[0])w=int(first_multiple_input[1])p=int(first_multiple_input[2])n=int(first_multiple_input[3])result=minimumPasses(m,w,p,n)fptr.write(str(result)+'\n')fptr.close()
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 →
Solution in Python 3