首页 > 其他 > 详细

53. Maximum Subarray

时间:2020-07-08 21:23:03      阅读:70      评论:0      收藏:0      [点我收藏+]

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

 

53. Maximum Subarray

原文:https://www.cnblogs.com/whatyouthink/p/13268856.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!