leetcode专题训练 53. Maximum Subarray

很简单的dp操作。

import numpy as np

class Solution:
    def maxSubArray(self, nums: List[int]) -> int:
        l = len(nums)
        dp = np.zeros(l)
        dp[0] = nums[0]
        for i in range(1, l):
            if dp[i-1] <= 0:
                dp[i] = nums[i]
            else:
                dp[i] = dp[i-1]+nums[i]
        return int(max(dp))
            
发布了201 篇原创文章 · 获赞 26 · 访问量 8万+

猜你喜欢

转载自blog.csdn.net/Ema1997/article/details/100785171
今日推荐