• + 1 comment
    def twoStacks(maxSum, a, b):
        # Write your code here
        i = j = s = 0
        
        while i < len(a) and s + a[i] <= maxSum: 
            s += a[i]
            i += 1
            
        n = maxn = i
        i -= 1
        
        while j < len(b):
            if s + b[j] <= maxSum:
                s += b[j]
                j += 1
                n += 1
                maxn = max(maxn, n)
            elif i >= 0:
                   s -= a[i]
                   i -= 1
                   n -= 1
            else:
                break
        
        return maxn
    
    • + 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
      
      • + 1 comment

        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