Sharing my Python solution. 9 lines


  • -1
    X
    class Solution:
        # @param A, a list of integers
        # @return an integer
        def maxSubArray(self, A):
            maximum = float("-inf")
            allsum = 0
            ptr = 0
            while ptr < len(A):
                allsum += A[ptr]
                if allsum > maximum: maximum = allsum
                if allsum < 0: allsum = 0
                ptr += 1
            return maximum

Log in to reply
 

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