• + 1 comment

    EASY SOLUTION IN PYTHON3 WITH TWO APPROACHES

    def equalStacks(h1, h2, h3):
        # Write your code here
        
        # FIRST APPROACH
        s1, s2, s3 = sum(h1), sum(h2), sum(h3)
        ptr_1, ptr_2, ptr_3 = 0, 0, 0
        
        while not s1 == s2 == s3:
            if s1>=s2 and s1>=s3:
                s1 -= h1[ptr_1]
                ptr_1 += 1
            elif s2>=s1 and s2>=s3:
                s2 -= h2[ptr_2]
                ptr_2 += 1
            elif s3>=s1 and s3>=s2:
                s3 -= h3[ptr_3]
                ptr_3 += 1
    
        return s1
        
        # SECOND APPROACH
        d1, d2, d3 = deque(h1), deque(h2), deque(h3)
        s1, s2, s3 = sum(h1), sum(h2), sum(h3)
        
        while not s1 == s2 == s3:
            if s1>=s2 and s1>=s3:
                s1 -= d1.popleft()
            elif s2>=s1 and s2>=s3:
                s2 -= d2.popleft()
            elif s3>=s1 and s3>=s2:
                s3 -= d3.popleft()
                
        return s1