• + 1 comment

    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

    while score < maxSum:
        head_a = a[0]
        head_b = b[0]
        if head_a > head_b and (score + head_b < maxSum):
            score += head_b
            b.pop(0)
        elif head_b > head_a and (score + head_a < maxSum):
            score += head_a
            a.pop(0)
        else:
            break
        counter += 1
    return counter
    
    • + 2 comments

      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).

      • + 0 comments

        Nice one

      • + 0 comments

        Although I get why its working, I feel that conceptually its misleading to talk about stacks. Technically you can only view the top of item of a stack or peek at without removing it but basically the solution is that you can peek at all the values if you want and then decide which values to remove.