Maximum Matrix Sum LeetCode solution in JavaScript

1
var maxMatrixSum = function (matrix) {
2
const MAX = Number.MAX_SAFE_INTEGER;
3

4
const m = matrix.length;
5
const n = matrix[0].length;
6

7
let negs = 0;
8

9
let totAbsSum = 0;
10
let minAbsNum = MAX;
11

12
for (let i = 0; i < n; ++i) {
13
for (let j = 0; j < n; ++j) {
14
if (matrix[i][j] < 0) ++negs;
15

16
totAbsSum += Math.abs(matrix[i][j]);
17
minAbsNum = Math.min(minAbsNum, Math.abs(matrix[i][j]));
18
}
19
}
20

21
if (negs % 2 === 1) totAbsSum -= 2 * minAbsNum;
22

23
return totAbsSum;
24
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0