diff options
-rw-r--r-- | maximum_matrix_sum.c | 40 | ||||
-rw-r--r-- | maximum_matrix_sum.py | 32 |
2 files changed, 72 insertions, 0 deletions
diff --git a/maximum_matrix_sum.c b/maximum_matrix_sum.c new file mode 100644 index 0000000..c0d3604 --- /dev/null +++ b/maximum_matrix_sum.c @@ -0,0 +1,40 @@ +// 1975. Maximum Matrix Sum +#include "leetcode.h" + +/* + * 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 maximise the summation of the + * matrix's elements. return maximum sum of the matrix's elements using the + * afforementioned operation. + */ + +long long maxMatrixSum(int **matrix, int matrixSize, int *matrixColSize) { + long long sum = 0; + int neg_cnt = 0, min = INT_MAX; + for (int i = 0; i < matrixSize; i++) + for (int j = 0; j < matrixSize; j++) { + if (matrix[i][j] < 0) + neg_cnt++; + if (abs(matrix[i][j]) < min) + min = abs(matrix[i][j]); + sum += abs(matrix[i][j]); + } + if (!(neg_cnt % 2)) + return sum; + return sum - (2 * min); +} + +int main() { + int m1i[2][2] = {{1, -1}, {-1, 1}}, + m2i[3][3] = {{1, 2, 3}, {-1, -2, -3}, {1, 2, 3}}; + struct two_d_arr m1, m2; + two_d_arr_init(&m1, 2, 2, m1i); + two_d_arr_init(&m2, 3, 3, m2i); + printf("%lld\n", maxMatrixSum(m1.arr, m1.row_size, m1.col_size)); // expect: 4 + printf("%lld\n", maxMatrixSum(m2.arr, m2.row_size, m2.col_size)); // expect: + // 16 + two_d_arr_free(&m1); + two_d_arr_free(&m2); +} diff --git a/maximum_matrix_sum.py b/maximum_matrix_sum.py new file mode 100644 index 0000000..1babfdf --- /dev/null +++ b/maximum_matrix_sum.py @@ -0,0 +1,32 @@ +# 1975. Maximum Matrix Sum + +""" +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 maximise the summation of the +matrix's elements. return maximum sum of the matrix's elements using the +afforementioned operation. +""" + + +class Solution(object): + def maxMatrixSum(self, matrix): + """ + :type matrix: List[List[int]] + :rtype: int + """ + ans, mult, val = 0, 0, float("inf") + for i in range(len(matrix)): + for j in range(len(matrix)): + ans += abs(matrix[i][j]) + val = min(val, abs(matrix[i][j])) + if matrix[i][j] < 0: + mult ^= 1 + return ans - 2 * mult * val + + +if __name__ == "__main__": + obj = Solution() + print(obj.maxMatrixSum(matrix=[[1, -1], [-1, 1]])) + print(obj.maxMatrixSum(matrix=[[1, 2, 3], [-1, -2, -3], [1, 2, 3]])) |