File tree Expand file tree Collapse file tree 1 file changed +23
-26
lines changed Expand file tree Collapse file tree 1 file changed +23
-26
lines changed Original file line number Diff line number Diff line change 2
2
* @param {number[] } nums
3
3
* @return {number }
4
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
- }
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
+ }
15
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
- }
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
+ }
30
27
31
- return Math . max ( Math . abs ( min ) , max ) ;
32
- } ;
28
+ return Math . max ( Math . abs ( min ) , max )
29
+ }
You can’t perform that action at this time.
0 commit comments