1.题目
2.思想
-
dp[i]
表示以nums[i]
结尾时获取的最大值 - 递推公式:
dp[i+1] = max(dp[i],0)+ num[i+1]
3.代码
class Solution:
def maxSubArray(self, nums: List[int]) -> int:
dp = [0] * len(nums)
dp[0] = nums[0]
res = dp[0]
for i in range(1,len(nums)):
if dp[i-1]>0:
dp[i] = dp[i-1] + nums[i]
else:
dp[i] = nums[i]
res = max(res,dp[i])
# print(dp)
return res