7171
7272否则,说明矩阵中有奇数个负数,最终一定会剩下一个负数,我们选择绝对值最小的数,将其变为负数,这样可以使得最终的和最大。
7373
74- 时间复杂度 $O(m\times n)$,其中 $m$ 和 $n$ 分别是矩阵的行数和列数。
74+ 时间复杂度 $O(m \times n)$,其中 $m$ 和 $n$ 分别是矩阵的行数和列数。空间复杂度 $O(1)$ 。
7575
7676<!-- tabs:start -->
7777
@@ -80,17 +80,15 @@ tags:
8080``` python
8181class Solution :
8282 def maxMatrixSum (self , matrix : List[List[int ]]) -> int :
83- s = cnt = 0
8483 mi = inf
84+ s = cnt = 0
8585 for row in matrix:
86- for v in row:
87- s += abs (v)
88- mi = min (mi, abs (v))
89- if v < 0 :
90- cnt += 1
91- if cnt % 2 == 0 or mi == 0 :
92- return s
93- return s - mi * 2
86+ for x in row:
87+ cnt += x < 0
88+ y = abs (x)
89+ mi = min (mi, y)
90+ s += y
91+ return s if cnt % 2 == 0 else s - mi * 2
9492```
9593
9694#### Java
@@ -99,21 +97,16 @@ class Solution:
9997class Solution {
10098 public long maxMatrixSum (int [][] matrix ) {
10199 long s = 0 ;
102- int cnt = 0 ;
103- int mi = Integer . MAX_VALUE ;
100+ int mi = 1 << 30 , cnt = 0 ;
104101 for (var row : matrix) {
105- for (var v : row) {
106- s += Math . abs(v);
107- mi = Math . min(mi, Math . abs(v));
108- if (v < 0 ) {
109- ++ cnt;
110- }
102+ for (int x : row) {
103+ cnt += x < 0 ? 1 : 0 ;
104+ int y = Math . abs(x);
105+ mi = Math . min(mi, y);
106+ s += y;
111107 }
112108 }
113- if (cnt % 2 == 0 || mi == 0 ) {
114- return s;
115- }
116- return s - mi * 2 ;
109+ return cnt % 2 == 0 ? s : s - mi * 2 ;
117110 }
118111}
119112```
@@ -125,16 +118,16 @@ class Solution {
125118public:
126119 long long maxMatrixSum(vector<vector<int >>& matrix) {
127120 long long s = 0;
128- int cnt = 0, mi = INT_MAX;
129- for (auto& row : matrix) {
130- for (int& v : row) {
131- s += abs(v);
132- mi = min(mi, abs(v));
133- cnt += v < 0;
121+ int mi = 1 << 30, cnt = 0;
122+ for (const auto& row : matrix) {
123+ for (int x : row) {
124+ cnt += x < 0 ? 1 : 0;
125+ int y = abs(x);
126+ mi = min(mi, y);
127+ s += y;
134128 }
135129 }
136- if (cnt % 2 == 0 || mi == 0) return s;
137- return s - mi * 2;
130+ return cnt % 2 == 0 ? s : s - mi * 2;
138131 }
139132};
140133```
@@ -143,28 +136,66 @@ public:
143136
144137```go
145138func maxMatrixSum(matrix [][]int) int64 {
146- s := 0
147- cnt, mi := 0, math.MaxInt32
139+ var s int64
140+ mi, cnt := 1<<30, 0
148141 for _, row := range matrix {
149- for _, v := range row {
150- s += abs(v)
151- mi = min(mi, abs(v))
152- if v < 0 {
142+ for _, x := range row {
143+ if x < 0 {
153144 cnt++
145+ x = -x
154146 }
147+ mi = min(mi, x)
148+ s += int64(x)
155149 }
156150 }
157- if cnt%2 == 1 {
158- s -= mi * 2
151+ if cnt%2 == 0 {
152+ return s
159153 }
160- return int64(s )
154+ return s - int64(mi*2 )
161155}
156+ ```
162157
163- func abs(x int) int {
164- if x < 0 {
165- return -x
166- }
167- return x
158+ #### TypeScript
159+
160+ ``` ts
161+ function maxMatrixSum(matrix : number [][]): number {
162+ let [s, cnt, mi] = [0 , 0 , Infinity ];
163+ for (const row of matrix ) {
164+ for (const x of row ) {
165+ if (x < 0 ) {
166+ ++ cnt ;
167+ }
168+ const y = Math .abs (x );
169+ s += y ;
170+ mi = Math .min (mi , y );
171+ }
172+ }
173+ return cnt % 2 === 0 ? s : s - 2 * mi ;
174+ }
175+ ```
176+
177+ #### Rust
178+
179+ ``` rust
180+ impl Solution {
181+ pub fn max_matrix_sum (matrix : Vec <Vec <i32 >>) -> i64 {
182+ let mut s = 0 ;
183+ let mut mi = i32 :: MAX ;
184+ let mut cnt = 0 ;
185+ for row in matrix {
186+ for & x in row . iter () {
187+ cnt += if x < 0 { 1 } else { 0 };
188+ let y = x . abs ();
189+ mi = mi . min (y );
190+ s += y as i64 ;
191+ }
192+ }
193+ if cnt % 2 == 0 {
194+ s
195+ } else {
196+ s - (mi as i64 * 2 )
197+ }
198+ }
168199}
169200```
170201
@@ -176,20 +207,18 @@ func abs(x int) int {
176207 * @return {number}
177208 */
178209var maxMatrixSum = function (matrix ) {
179- let cnt = 0 ;
180- let s = 0 ;
181- let mi = Infinity ;
210+ let [s, cnt, mi] = [0 , 0 , Infinity ];
182211 for (const row of matrix) {
183- for (const v of row) {
184- s += Math .abs (v);
185- mi = Math .min (mi, Math .abs (v));
186- cnt += v < 0 ;
212+ for (const x of row) {
213+ if (x < 0 ) {
214+ ++ cnt;
215+ }
216+ const y = Math .abs (x);
217+ s += y;
218+ mi = Math .min (mi, y);
187219 }
188220 }
189- if (cnt % 2 == 0 ) {
190- return s;
191- }
192- return s - mi * 2 ;
221+ return cnt % 2 === 0 ? s : s - 2 * mi;
193222};
194223```
195224
0 commit comments