Minor tweak to make a super-clean Python 5-liner


  • 0
    D

    The problem is defined to have at least one element in the input array, so it's slightly nicer to init current and best max values to the first element and then iterate a slice of the rest of the input.

    class Solution(object):
        def maxSubArray(self, nums):
            """
            :type nums: List[int]
            :rtype: int
            """
            bestMax = currMax = nums[0]
            for n in nums[1:]:
                currMax = max(n, n + currMax)
                bestMax = max(currMax, bestMax)
            return bestMax
    

Log in to reply
 

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