Normally, you would try a O(N^2) brute force.
but actually, you know we are finding a interval sum and take a absolute value.
interval sum i, j = prefixsum[j] - prefixsum[i - 1];
so if we want the max interval sum, we need to find the minimum prefixSum and max prefix sum.
so we just need to calculate the prefix sum, and updating the min and max prefixsum, and return their difference.
class Solution {
public:
int maxAbsoluteSum(vector<int>& A) {
int s = 0, mi = 0, ma = 0;
for (int& a: A) {
s += a;
mi = min(mi, s);
ma = max(ma, s);
}
return ma - mi;
}
};