Share my python solution


  • 0
    M
    class Solution:
        # @param A, a list of integers
        # @return an integer
        def maxSubArray(self, A):
            # assume largest sum is 0 for now. 
            # If largest numb is negative, that's the largest sum
            largest_sum = 0
            current_sum = 0
            largest_num = A[0]
            for item in A:
                if item > largest_num:
                    largest_num = item
                current_sum += item
                if current_sum < 0:
                    # abandon all together
                    current_sum = 0
                if current_sum > largest_sum:
                    largest_sum = current_sum
            if largest_num < 0:
                largest_sum = largest_num
            return largest_sum

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.