We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent a47487c commit d9576b0Copy full SHA for d9576b0
C++/maximum-matrix-sum.cpp
@@ -0,0 +1,20 @@
1
+// Time: O(n^2)
2
+// Space: O(1)
3
+
4
+class Solution {
5
+public:
6
+ long long maxMatrixSum(vector<vector<int>>& matrix) {
7
+ int64_t abs_total = 0, min_abs_val = numeric_limits<int64_t>::max();
8
+ bool is_neg_cnt_odd = false;
9
+ for (int i = 0; i < size(matrix); ++i) {
10
+ for (int j = 0; j < size(matrix[0]); ++j) {
11
+ abs_total += abs(matrix[i][j]);
12
+ min_abs_val = min(min_abs_val, labs(matrix[i][j]));
13
+ if (matrix[i][j] < 0) {
14
+ is_neg_cnt_odd = !is_neg_cnt_odd;
15
+ }
16
17
18
+ return !is_neg_cnt_odd ? abs_total : abs_total - 2 * min_abs_val;
19
20
+};
0 commit comments