Skip to content

Commit 4c2cf58

Browse files
authored
Create 1749-maximum-absolute-sum-of-any-subarray.js
1 parent e31727c commit 4c2cf58

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {number}
4+
*/
5+
const maxAbsoluteSum = function(nums) {
6+
let min = 0, max = 0;
7+
let positiveSum = 0, negativeSum = 0;
8+
for (let num of nums)
9+
{
10+
positiveSum += num;
11+
if (positiveSum > max)
12+
{
13+
max = positiveSum;
14+
}
15+
16+
if (positiveSum < 0)
17+
{
18+
positiveSum = 0;
19+
}
20+
negativeSum += num;
21+
if (negativeSum < min)
22+
{
23+
min = negativeSum;
24+
}
25+
if (negativeSum > 0)
26+
{
27+
negativeSum = 0;
28+
}
29+
}
30+
31+
return Math.max(Math.abs(min), max);
32+
};

0 commit comments

Comments
 (0)