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.
deftwoStacks(maxSum,a,b):# Write your code herei=j=s=0whilei<len(a)ands+a[i]<=maxSum:s+=a[i]i+=1n=maxn=ii-=1whilej<len(b):ifs+b[j]<=maxSum:s+=b[j]j+=1n+=1maxn=max(maxn,n)elifi>=0:s-=a[i]i-=1n-=1else:breakreturnmaxn
Could you explain the code please?
I got up to here but I dont take into account g games so not sure what Im missing as I pass the first test case but fail others.
def twoStacks(maxSum, a, b):
score = 0
counter = 0
selectig the smallest top of stack from a, and b doesn't always guarantee the maxmum number of selections. I know this seems counter-intuitive but consider the following example:
maxSum = 10
a = [5, 6]
b = [6, 4]
The correct answer is 2 selections (b[0] + b[1] = 6 + 4 = 10). But you code will produce only 1 selection (a[0] = 5).
Game of Two Stacks
You are viewing a single comment's thread. Return to all comments →
Could you explain the code please? I got up to here but I dont take into account g games so not sure what Im missing as I pass the first test case but fail others.
def twoStacks(maxSum, a, b): score = 0 counter = 0
selectig the smallest top of stack from a, and b doesn't always guarantee the maxmum number of selections. I know this seems counter-intuitive but consider the following example: maxSum = 10 a = [5, 6] b = [6, 4]
The correct answer is 2 selections (b[0] + b[1] = 6 + 4 = 10). But you code will produce only 1 selection (a[0] = 5).
Nice one