Given an integer array nums
, find the contiguous subarray (containing at least one number) which has the largest sum and return its sum.
最大连续子段和,经典动态规划题。
维护一个全局最大和当前最大,每次更新当前最大都更新下全局最大
class Solution(object): def maxSubArray(self, nums): """ :type nums: List[int] :rtype: int """ max_sum = min(nums) max_current_sum = 0 for value in nums: max_current_sum += value max_sum = max(max_sum, max_current_sum) if max_current_sum < 0: max_current_sum = 0 return max_sum