diff --git a/solution/1900-1999/1975.Maximum Matrix Sum/README.md b/solution/1900-1999/1975.Maximum Matrix Sum/README.md index 6eafd918560c2..aa34139ee3881 100644 --- a/solution/1900-1999/1975.Maximum Matrix Sum/README.md +++ b/solution/1900-1999/1975.Maximum Matrix Sum/README.md @@ -71,7 +71,7 @@ tags: 否则,说明矩阵中有奇数个负数,最终一定会剩下一个负数,我们选择绝对值最小的数,将其变为负数,这样可以使得最终的和最大。 -时间复杂度 $O(m\times n)$,其中 $m$ 和 $n$ 分别是矩阵的行数和列数。 +时间复杂度 $O(m \times n)$,其中 $m$ 和 $n$ 分别是矩阵的行数和列数。空间复杂度 $O(1)$。 @@ -80,17 +80,15 @@ tags: ```python class Solution: def maxMatrixSum(self, matrix: List[List[int]]) -> int: - s = cnt = 0 mi = inf + s = cnt = 0 for row in matrix: - for v in row: - s += abs(v) - mi = min(mi, abs(v)) - if v < 0: - cnt += 1 - if cnt % 2 == 0 or mi == 0: - return s - return s - mi * 2 + for x in row: + cnt += x < 0 + y = abs(x) + mi = min(mi, y) + s += y + return s if cnt % 2 == 0 else s - mi * 2 ``` #### Java @@ -99,21 +97,16 @@ class Solution: class Solution { public long maxMatrixSum(int[][] matrix) { long s = 0; - int cnt = 0; - int mi = Integer.MAX_VALUE; + int mi = 1 << 30, cnt = 0; for (var row : matrix) { - for (var v : row) { - s += Math.abs(v); - mi = Math.min(mi, Math.abs(v)); - if (v < 0) { - ++cnt; - } + for (int x : row) { + cnt += x < 0 ? 1 : 0; + int y = Math.abs(x); + mi = Math.min(mi, y); + s += y; } } - if (cnt % 2 == 0 || mi == 0) { - return s; - } - return s - mi * 2; + return cnt % 2 == 0 ? s : s - mi * 2; } } ``` @@ -125,16 +118,16 @@ class Solution { public: long long maxMatrixSum(vector>& matrix) { long long s = 0; - int cnt = 0, mi = INT_MAX; - for (auto& row : matrix) { - for (int& v : row) { - s += abs(v); - mi = min(mi, abs(v)); - cnt += v < 0; + int mi = 1 << 30, cnt = 0; + for (const auto& row : matrix) { + for (int x : row) { + cnt += x < 0 ? 1 : 0; + int y = abs(x); + mi = min(mi, y); + s += y; } } - if (cnt % 2 == 0 || mi == 0) return s; - return s - mi * 2; + return cnt % 2 == 0 ? s : s - mi * 2; } }; ``` @@ -143,28 +136,66 @@ public: ```go func maxMatrixSum(matrix [][]int) int64 { - s := 0 - cnt, mi := 0, math.MaxInt32 + var s int64 + mi, cnt := 1<<30, 0 for _, row := range matrix { - for _, v := range row { - s += abs(v) - mi = min(mi, abs(v)) - if v < 0 { + for _, x := range row { + if x < 0 { cnt++ + x = -x } + mi = min(mi, x) + s += int64(x) } } - if cnt%2 == 1 { - s -= mi * 2 + if cnt%2 == 0 { + return s } - return int64(s) + return s - int64(mi*2) } +``` -func abs(x int) int { - if x < 0 { - return -x - } - return x +#### TypeScript + +```ts +function maxMatrixSum(matrix: number[][]): number { + let [s, cnt, mi] = [0, 0, Infinity]; + for (const row of matrix) { + for (const x of row) { + if (x < 0) { + ++cnt; + } + const y = Math.abs(x); + s += y; + mi = Math.min(mi, y); + } + } + return cnt % 2 === 0 ? s : s - 2 * mi; +} +``` + +#### Rust + +```rust +impl Solution { + pub fn max_matrix_sum(matrix: Vec>) -> i64 { + let mut s = 0; + let mut mi = i32::MAX; + let mut cnt = 0; + for row in matrix { + for &x in row.iter() { + cnt += if x < 0 { 1 } else { 0 }; + let y = x.abs(); + mi = mi.min(y); + s += y as i64; + } + } + if cnt % 2 == 0 { + s + } else { + s - (mi as i64 * 2) + } + } } ``` @@ -176,20 +207,18 @@ func abs(x int) int { * @return {number} */ var maxMatrixSum = function (matrix) { - let cnt = 0; - let s = 0; - let mi = Infinity; + let [s, cnt, mi] = [0, 0, Infinity]; for (const row of matrix) { - for (const v of row) { - s += Math.abs(v); - mi = Math.min(mi, Math.abs(v)); - cnt += v < 0; + for (const x of row) { + if (x < 0) { + ++cnt; + } + const y = Math.abs(x); + s += y; + mi = Math.min(mi, y); } } - if (cnt % 2 == 0) { - return s; - } - return s - mi * 2; + return cnt % 2 === 0 ? s : s - 2 * mi; }; ``` diff --git a/solution/1900-1999/1975.Maximum Matrix Sum/README_EN.md b/solution/1900-1999/1975.Maximum Matrix Sum/README_EN.md index ca957bd75468d..719d2b0d339b9 100644 --- a/solution/1900-1999/1975.Maximum Matrix Sum/README_EN.md +++ b/solution/1900-1999/1975.Maximum Matrix Sum/README_EN.md @@ -65,7 +65,13 @@ tags: -### Solution 1 +### Solution 1: Greedy + +If there is a zero in the matrix, or the number of negative numbers in the matrix is even, then the maximum sum is the sum of the absolute values of all elements in the matrix. + +Otherwise, if there are an odd number of negative numbers in the matrix, there will be one negative number left in the end. We choose the number with the smallest absolute value and make it negative, so that the final sum is maximized. + +The time complexity is $O(m \times n)$, where $m$ and $n$ are the number of rows and columns of the matrix, respectively. The space complexity is $O(1)$. @@ -74,17 +80,15 @@ tags: ```python class Solution: def maxMatrixSum(self, matrix: List[List[int]]) -> int: - s = cnt = 0 mi = inf + s = cnt = 0 for row in matrix: - for v in row: - s += abs(v) - mi = min(mi, abs(v)) - if v < 0: - cnt += 1 - if cnt % 2 == 0 or mi == 0: - return s - return s - mi * 2 + for x in row: + cnt += x < 0 + y = abs(x) + mi = min(mi, y) + s += y + return s if cnt % 2 == 0 else s - mi * 2 ``` #### Java @@ -93,21 +97,16 @@ class Solution: class Solution { public long maxMatrixSum(int[][] matrix) { long s = 0; - int cnt = 0; - int mi = Integer.MAX_VALUE; + int mi = 1 << 30, cnt = 0; for (var row : matrix) { - for (var v : row) { - s += Math.abs(v); - mi = Math.min(mi, Math.abs(v)); - if (v < 0) { - ++cnt; - } + for (int x : row) { + cnt += x < 0 ? 1 : 0; + int y = Math.abs(x); + mi = Math.min(mi, y); + s += y; } } - if (cnt % 2 == 0 || mi == 0) { - return s; - } - return s - mi * 2; + return cnt % 2 == 0 ? s : s - mi * 2; } } ``` @@ -119,16 +118,16 @@ class Solution { public: long long maxMatrixSum(vector>& matrix) { long long s = 0; - int cnt = 0, mi = INT_MAX; - for (auto& row : matrix) { - for (int& v : row) { - s += abs(v); - mi = min(mi, abs(v)); - cnt += v < 0; + int mi = 1 << 30, cnt = 0; + for (const auto& row : matrix) { + for (int x : row) { + cnt += x < 0 ? 1 : 0; + int y = abs(x); + mi = min(mi, y); + s += y; } } - if (cnt % 2 == 0 || mi == 0) return s; - return s - mi * 2; + return cnt % 2 == 0 ? s : s - mi * 2; } }; ``` @@ -137,28 +136,66 @@ public: ```go func maxMatrixSum(matrix [][]int) int64 { - s := 0 - cnt, mi := 0, math.MaxInt32 + var s int64 + mi, cnt := 1<<30, 0 for _, row := range matrix { - for _, v := range row { - s += abs(v) - mi = min(mi, abs(v)) - if v < 0 { + for _, x := range row { + if x < 0 { cnt++ + x = -x } + mi = min(mi, x) + s += int64(x) } } - if cnt%2 == 1 { - s -= mi * 2 + if cnt%2 == 0 { + return s } - return int64(s) + return s - int64(mi*2) } +``` -func abs(x int) int { - if x < 0 { - return -x - } - return x +#### TypeScript + +```ts +function maxMatrixSum(matrix: number[][]): number { + let [s, cnt, mi] = [0, 0, Infinity]; + for (const row of matrix) { + for (const x of row) { + if (x < 0) { + ++cnt; + } + const y = Math.abs(x); + s += y; + mi = Math.min(mi, y); + } + } + return cnt % 2 === 0 ? s : s - 2 * mi; +} +``` + +#### Rust + +```rust +impl Solution { + pub fn max_matrix_sum(matrix: Vec>) -> i64 { + let mut s = 0; + let mut mi = i32::MAX; + let mut cnt = 0; + for row in matrix { + for &x in row.iter() { + cnt += if x < 0 { 1 } else { 0 }; + let y = x.abs(); + mi = mi.min(y); + s += y as i64; + } + } + if cnt % 2 == 0 { + s + } else { + s - (mi as i64 * 2) + } + } } ``` @@ -170,20 +207,18 @@ func abs(x int) int { * @return {number} */ var maxMatrixSum = function (matrix) { - let cnt = 0; - let s = 0; - let mi = Infinity; + let [s, cnt, mi] = [0, 0, Infinity]; for (const row of matrix) { - for (const v of row) { - s += Math.abs(v); - mi = Math.min(mi, Math.abs(v)); - cnt += v < 0; + for (const x of row) { + if (x < 0) { + ++cnt; + } + const y = Math.abs(x); + s += y; + mi = Math.min(mi, y); } } - if (cnt % 2 == 0) { - return s; - } - return s - mi * 2; + return cnt % 2 === 0 ? s : s - 2 * mi; }; ``` diff --git a/solution/1900-1999/1975.Maximum Matrix Sum/Solution.cpp b/solution/1900-1999/1975.Maximum Matrix Sum/Solution.cpp index 21fe704f1db68..62ed55c02a8c3 100644 --- a/solution/1900-1999/1975.Maximum Matrix Sum/Solution.cpp +++ b/solution/1900-1999/1975.Maximum Matrix Sum/Solution.cpp @@ -2,15 +2,15 @@ class Solution { public: long long maxMatrixSum(vector>& matrix) { long long s = 0; - int cnt = 0, mi = INT_MAX; - for (auto& row : matrix) { - for (int& v : row) { - s += abs(v); - mi = min(mi, abs(v)); - cnt += v < 0; + int mi = 1 << 30, cnt = 0; + for (const auto& row : matrix) { + for (int x : row) { + cnt += x < 0 ? 1 : 0; + int y = abs(x); + mi = min(mi, y); + s += y; } } - if (cnt % 2 == 0 || mi == 0) return s; - return s - mi * 2; + return cnt % 2 == 0 ? s : s - mi * 2; } -}; \ No newline at end of file +}; diff --git a/solution/1900-1999/1975.Maximum Matrix Sum/Solution.go b/solution/1900-1999/1975.Maximum Matrix Sum/Solution.go index dd18c144d8358..d4d64801f8179 100644 --- a/solution/1900-1999/1975.Maximum Matrix Sum/Solution.go +++ b/solution/1900-1999/1975.Maximum Matrix Sum/Solution.go @@ -1,24 +1,18 @@ func maxMatrixSum(matrix [][]int) int64 { - s := 0 - cnt, mi := 0, math.MaxInt32 + var s int64 + mi, cnt := 1<<30, 0 for _, row := range matrix { - for _, v := range row { - s += abs(v) - mi = min(mi, abs(v)) - if v < 0 { + for _, x := range row { + if x < 0 { cnt++ + x = -x } + mi = min(mi, x) + s += int64(x) } } - if cnt%2 == 1 { - s -= mi * 2 + if cnt%2 == 0 { + return s } - return int64(s) + return s - int64(mi*2) } - -func abs(x int) int { - if x < 0 { - return -x - } - return x -} \ No newline at end of file diff --git a/solution/1900-1999/1975.Maximum Matrix Sum/Solution.java b/solution/1900-1999/1975.Maximum Matrix Sum/Solution.java index 5273337b6cf9f..125e868d8e116 100644 --- a/solution/1900-1999/1975.Maximum Matrix Sum/Solution.java +++ b/solution/1900-1999/1975.Maximum Matrix Sum/Solution.java @@ -1,20 +1,15 @@ class Solution { public long maxMatrixSum(int[][] matrix) { long s = 0; - int cnt = 0; - int mi = Integer.MAX_VALUE; + int mi = 1 << 30, cnt = 0; for (var row : matrix) { - for (var v : row) { - s += Math.abs(v); - mi = Math.min(mi, Math.abs(v)); - if (v < 0) { - ++cnt; - } + for (int x : row) { + cnt += x < 0 ? 1 : 0; + int y = Math.abs(x); + mi = Math.min(mi, y); + s += y; } } - if (cnt % 2 == 0 || mi == 0) { - return s; - } - return s - mi * 2; + return cnt % 2 == 0 ? s : s - mi * 2; } -} \ No newline at end of file +} diff --git a/solution/1900-1999/1975.Maximum Matrix Sum/Solution.js b/solution/1900-1999/1975.Maximum Matrix Sum/Solution.js index d7adb108cd59a..8504e8af75e7c 100644 --- a/solution/1900-1999/1975.Maximum Matrix Sum/Solution.js +++ b/solution/1900-1999/1975.Maximum Matrix Sum/Solution.js @@ -3,18 +3,16 @@ * @return {number} */ var maxMatrixSum = function (matrix) { - let cnt = 0; - let s = 0; - let mi = Infinity; + let [s, cnt, mi] = [0, 0, Infinity]; for (const row of matrix) { - for (const v of row) { - s += Math.abs(v); - mi = Math.min(mi, Math.abs(v)); - cnt += v < 0; + for (const x of row) { + if (x < 0) { + ++cnt; + } + const y = Math.abs(x); + s += y; + mi = Math.min(mi, y); } } - if (cnt % 2 == 0) { - return s; - } - return s - mi * 2; + return cnt % 2 === 0 ? s : s - 2 * mi; }; diff --git a/solution/1900-1999/1975.Maximum Matrix Sum/Solution.py b/solution/1900-1999/1975.Maximum Matrix Sum/Solution.py index 785c2cc2fdb49..3b2f437128526 100644 --- a/solution/1900-1999/1975.Maximum Matrix Sum/Solution.py +++ b/solution/1900-1999/1975.Maximum Matrix Sum/Solution.py @@ -1,13 +1,11 @@ class Solution: def maxMatrixSum(self, matrix: List[List[int]]) -> int: - s = cnt = 0 mi = inf + s = cnt = 0 for row in matrix: - for v in row: - s += abs(v) - mi = min(mi, abs(v)) - if v < 0: - cnt += 1 - if cnt % 2 == 0 or mi == 0: - return s - return s - mi * 2 + for x in row: + cnt += x < 0 + y = abs(x) + mi = min(mi, y) + s += y + return s if cnt % 2 == 0 else s - mi * 2 diff --git a/solution/1900-1999/1975.Maximum Matrix Sum/Solution.rs b/solution/1900-1999/1975.Maximum Matrix Sum/Solution.rs new file mode 100644 index 0000000000000..1d0fbf9914da9 --- /dev/null +++ b/solution/1900-1999/1975.Maximum Matrix Sum/Solution.rs @@ -0,0 +1,20 @@ +impl Solution { + pub fn max_matrix_sum(matrix: Vec>) -> i64 { + let mut s = 0; + let mut mi = i32::MAX; + let mut cnt = 0; + for row in matrix { + for &x in row.iter() { + cnt += if x < 0 { 1 } else { 0 }; + let y = x.abs(); + mi = mi.min(y); + s += y as i64; + } + } + if cnt % 2 == 0 { + s + } else { + s - (mi as i64 * 2) + } + } +} diff --git a/solution/1900-1999/1975.Maximum Matrix Sum/Solution.ts b/solution/1900-1999/1975.Maximum Matrix Sum/Solution.ts new file mode 100644 index 0000000000000..a292522bafff3 --- /dev/null +++ b/solution/1900-1999/1975.Maximum Matrix Sum/Solution.ts @@ -0,0 +1,14 @@ +function maxMatrixSum(matrix: number[][]): number { + let [s, cnt, mi] = [0, 0, Infinity]; + for (const row of matrix) { + for (const x of row) { + if (x < 0) { + ++cnt; + } + const y = Math.abs(x); + s += y; + mi = Math.min(mi, y); + } + } + return cnt % 2 === 0 ? s : s - 2 * mi; +}