[Java/Python 3] odd or even occurrences of negatives, w/ brief explanation and analysis.
If there are even occurrences of negatives, we can always change them to positives; otherwise, we can change the number with minimum abs to negative, and others
If there are even occurrences of negatives, we can always change them to positives; otherwise, we can change the number with minimum `abs` to negative, and others to positive.\n\n----\n\n**Q & A**\n\nQ: Why are we subtracting `2 * minVal` from the ans when there are odd number of negative elements?\nA: Because the sum of absolute values, `absTotal` was already added to the final sum here: `absTotal += Math.abs(matrix[i][j]);` That\'s why we have to subtract it twice.\n\n**End of Q & A**\n\n----\n\nThe followings are quoted from geeks4geeks:\n\nApproach: To maximize the sum of the given matrix, perform the given operations such that the smallest element in a row or column is negative (if it is not possible to make all row and column elements positive). Follow the steps below to maximize the sum: \n\n\n1. Let the number of cells having negative values be x. If x is 0 i.e., there are no negative values, then the sum of the matrix is already maximum possible.\n2. Otherwise, take any negative value from the matrix and perform the given operations on that element and any of its adjacent cells. This results in the chosen element becoming positive.\n3. Now if the value of the chosen adjacent element is negative, then it will become positive or vice versa. This way, in each turn two negative values can be made positive. Now following two cases arise:\n\nIf x is even: In this case, after x/2 turns, all the negative values can be made positive. Therefore, the maximum sum possible is equal to the sum of the absolute values of all cells.\n\nIf x is odd: In this case, there will be one negative value left after performing operations in the manner described above. Now, to maximize the sum, the value getting subtracted or having a negative sign needs to be minimized. To do this, move the negative sign to the cell having minimum absolute value, say minVal. Therefore, the maximum sum possible is sum of the absolute values of all cells \u2013 2*minVal.\n\n\n```java\n    public long maxMatrixSum(int[][] matrix) {\n        long absTotal = 0, negative = 0, mi = Long.MAX_VALUE;\n        for (int r = 0; r < matrix.length; ++r) {\n            for (int c = 0; c < matrix.length; ++c) {\n                if (matrix[r][c] < 0) {\n                    ++negative;\n                }\n                absTotal += Math.abs(matrix[r][c]);\n                mi = Math.min(mi, Math.abs(matrix[r][c]));\n            }\n        } \n        return absTotal - (negative % 2 == 0 ? 0 : 2 * mi);\n    }\n```\n```python\n    def maxMatrixSum(self, matrix: List[List[int]]) -> int:\n        abs_total = neg = 0\n        mi= math.inf\n        for row in matrix:\n            for num in row:\n                abs_total += abs(num)\n                if num < 0:\n                    neg += 1\n                mi = min(mi, abs(num))\n        return abs_total if neg % 2 == 0 else abs_total - 2 * mi\n```\n**Analysis:**\nTime: `O(n * n)`, space: `O(1)`, where `n = matrix.length`.\n\n```python\n    def maxMatrixSum(self, matrix: List[List[int]]) -> int:\n        arr = [num for row in matrix for num in row]\n        abs_total = sum(map(abs, arr))\n        neg = sum([num  < 0 for num in arr])\n        mi= min(map(abs, arr))\n        return abs_total if neg % 2 == 0 else abs_total - 2 * mi\n```\n\n**Analysis:**\nTime & space: `O(n * n)`, where `n = matrix.length`.
[]