Skip to content

Commit 59fea93

Browse files
authored
Update 1749-maximum-absolute-sum-of-any-subarray.js
1 parent 2fa5161 commit 59fea93

File tree

1 file changed

+23
-26
lines changed

1 file changed

+23
-26
lines changed

1749-maximum-absolute-sum-of-any-subarray.js

Lines changed: 23 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,28 @@
22
* @param {number[]} nums
33
* @return {number}
44
*/
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-
}
5+
const maxAbsoluteSum = function (nums) {
6+
let min = Infinity,
7+
max = -Infinity
8+
let positiveSum = 0,
9+
negativeSum = 0
10+
for (let num of nums) {
11+
positiveSum += num
12+
if (positiveSum > max) {
13+
max = positiveSum
14+
}
1515

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-
}
16+
if (positiveSum < 0) {
17+
positiveSum = 0
18+
}
19+
negativeSum += num
20+
if (negativeSum < min) {
21+
min = negativeSum
22+
}
23+
if (negativeSum > 0) {
24+
negativeSum = 0
25+
}
26+
}
3027

31-
return Math.max(Math.abs(min), max);
32-
};
28+
return Math.max(Math.abs(min), max)
29+
}

0 commit comments

Comments
 (0)