题目链接 | 1749. 任意子数组和的绝对值的最大值 |
---|---|
思路 | 前缀和/动态规划-最大子数组和-简单变体 |
题解链接 | 两种方法:动态规划/前缀和(附题单!Python/Java/C++/Go/JS) |
关键点 | 无 |
时间复杂度 | \(O(n)\) |
空间复杂度 | \(O(1)\) |
代码实现(动态规划):
class Solution:
def maxAbsoluteSum(self, nums: List[int]) -> int:
answer = maxv = minv = 0
for num in nums:
maxv = max(maxv, 0) + num
minv = min(minv, 0) + num
answer = max(answer, maxv, -minv)
return answer
标签:任意子,最大值,maxv,num,1749,answer,minv
From: https://www.cnblogs.com/WrRan/p/18419432