An elegant DP solution in python.

  • 0
    def maxSubArray(self, nums):
            if not nums: return 0
            dp = {0: nums[0]}
            ans = dp[0]
            for i in range(1, len(nums)):
                dp[i] = dp[i-1] + nums[i] if dp[i-1] > 0 else nums[i]
                ans = max(dp[i], ans)
            return ans

Log in to reply

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