-
Notifications
You must be signed in to change notification settings - Fork 30
/
Maximum Matrix Sum.cpp
38 lines (26 loc) · 1.09 KB
/
Maximum Matrix Sum.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/*
Solution by Rahul Surana
***********************************************************
You are given an n x n integer matrix. You can do the following operation any number of times:
Choose any two adjacent elements of matrix and multiply each of them by -1.
Two elements are considered adjacent if and only if they share a border.
Your goal is to maximize the summation of the matrix's elements. Return the maximum sum of the matrix's elements using the operation mentioned above.
***********************************************************
*/
class Solution {
public:
long long maxMatrixSum(vector<vector<int>>& matrix) {
int count = 0;
int min = 100001;
long long sum = 0;
for(int i = 0; i < matrix.size(); i++){
for(int j = 0; j < matrix[0].size(); j++){
sum += abs(matrix[i][j]);
if(matrix[i][j] < 0) count++;
if(min > abs(matrix[i][j])) min = abs(matrix[i][j]);
}
}
if(count %2) sum -= (2*min);
return sum;
}
};