[
dp
array
]
Leetcode 1749. Maximum Absolute Sum of Any Subarray
Problem statement
https://leetcode.com/problems/maximum-absolute-sum-of-any-subarray/
Solution
Just use Kadane twice.
Complexity
It is O(n)
for time and space.
Code
class Solution:
def maxAbsoluteSum(self, A):
def kadane(nums):
dp = [0]*len(nums)
dp[0] = nums[0]
for i in range(1, len(nums)):
dp[i] = max(nums[i], nums[i] + dp[i-1])
return max(dp)
return max(kadane(A), kadane([-x for x in A]))