Skip to content

Commit 87fe60e

Browse files
committed
[LeetCode Sync] Runtime - 0 ms (100.00%), Memory - 38.5 MB (31.40%)
1 parent bcc3637 commit 87fe60e

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed

2089-maximum-matrix-sum/README.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<p>You are given an <code>n x n</code> integer <code>matrix</code>. You can do the following operation <strong>any</strong> number of times:</p>
2+
3+
<ul>
4+
<li>Choose any two <strong>adjacent</strong> elements of <code>matrix</code> and <strong>multiply</strong> each of them by <code>-1</code>.</li>
5+
</ul>
6+
7+
<p>Two elements are considered <strong>adjacent</strong> if and only if they share a <strong>border</strong>.</p>
8+
9+
<p>Your goal is to <strong>maximize</strong> the summation of the matrix&#39;s elements. Return <em>the <strong>maximum</strong> sum of the matrix&#39;s elements using the operation mentioned above.</em></p>
10+
11+
<p>&nbsp;</p>
12+
<p><strong class="example">Example 1:</strong></p>
13+
<img alt="" src="https://assets.leetcode.com/uploads/2021/07/16/pc79-q2ex1.png" style="width: 401px; height: 81px;" />
14+
<pre>
15+
<strong>Input:</strong> matrix = [[1,-1],[-1,1]]
16+
<strong>Output:</strong> 4
17+
<b>Explanation:</b> We can follow the following steps to reach sum equals 4:
18+
- Multiply the 2 elements in the first row by -1.
19+
- Multiply the 2 elements in the first column by -1.
20+
</pre>
21+
22+
<p><strong class="example">Example 2:</strong></p>
23+
<img alt="" src="https://assets.leetcode.com/uploads/2021/07/16/pc79-q2ex2.png" style="width: 321px; height: 121px;" />
24+
<pre>
25+
<strong>Input:</strong> matrix = [[1,2,3],[-1,-2,-3],[1,2,3]]
26+
<strong>Output:</strong> 16
27+
<b>Explanation:</b> We can follow the following step to reach sum equals 16:
28+
- Multiply the 2 last elements in the second row by -1.
29+
</pre>
30+
31+
<p>&nbsp;</p>
32+
<p><strong>Constraints:</strong></p>
33+
34+
<ul>
35+
<li><code>n == matrix.length == matrix[i].length</code></li>
36+
<li><code>2 &lt;= n &lt;= 250</code></li>
37+
<li><code>-10<sup>5</sup> &lt;= matrix[i][j] &lt;= 10<sup>5</sup></code></li>
38+
</ul>
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
class Solution {
2+
public:
3+
long long maxMatrixSum(vector<vector<int>>& matrix) {
4+
// count number of negative signs
5+
long long neg_cnt = 0, ans = 0, size = matrix.size(), minn = 1000000;
6+
7+
// keep track of absolute sum of all values.
8+
for (int i = 0; i < size; ++i)
9+
for (int j = 0; j < size; ++j) {
10+
ans += abs(matrix[i][j]);
11+
12+
// check if the given element is negative
13+
if (matrix[i][j] < 0)
14+
neg_cnt++;
15+
16+
// keep track of the most minimum value absolute
17+
minn = minn < abs(matrix[i][j])? minn: abs(matrix[i][j]);
18+
}
19+
20+
// if negative signs are even, answer is sum of all elements
21+
if (neg_cnt % 2 == 0)
22+
return ans;
23+
24+
// otherwise, the most minimum value is subtracted (2x)
25+
else
26+
return ans - 2*minn;
27+
}
28+
};

0 commit comments

Comments
 (0)