[Swift, C++, Python] Very simple with explanation. 0ms 100%
Intuition\nCan we convert all numbers from negative to positive? Yes, but only if there are an even number of negative numbers. If there are an odd number of ne
# Intuition\nCan we convert all numbers from negative to positive? Yes, but only if there are an even number of negative numbers. If there are an odd number of negatives, then one number must remain negative. However, if we have a positive number with absolute value is less than that of a negative number, we can swap them. \n\n*For example:*\nThe smallest negative value is -5, and the smallest positive value is 1. They will become 5 and -1 respectively.\n\n# Approach\nSimply convert all values to positive and calculate their sum. We need to keep track of whether there are an odd or even number of initial negative values. Additionally, we need to identify the smallest value in our array, as if there were an odd count of negative values, this smallest value needs to be subtracted from the total sum. (Two times, because we added it before, and now its negative)\n\n# Complexity\n- Time complexity:\nO(n)\n\n- Space complexity:\nO(1)\n\n![Screenshot 2024-11-24 153341.png](https://assets.leetcode.com/users/images/b9522326-6175-404b-9713-c2c6b4d1bf04_1732454818.2341938.png)\n\n# Code Swift\n```swift []\nclass Solution {\n    func maxMatrixSum(_ matrix: [[Int]]) -> Int {\n        var m = Int.max\n        var even = true\n        var sum = 0\n\n        for y in 0 ..< matrix.count {\n            for x in 0 ..< matrix.count {\n                if matrix[y][x] < 0 {\n                    even = !even\n                }\n                var n = matrix[y][x] < 0 ? -1 * matrix[y][x] : matrix[y][x]\n                m = min(m,n)\n                sum += n\n            }\n        }\n\n        if !even {\n            sum -= m * 2\n        }\n\n        return sum\n    }\n}\n```\n\n# Code C++\n```cpp []\nclass Solution {\npublic:\n    long long maxMatrixSum(vector<vector<int>>& matrix) {\n        int m = INT32_MAX;\n        int even = true;\n        long long sum = 0;\n\n        for (int y = 0; y < matrix.size(); y++)\n        {\n            for (int x = 0; x < matrix.size(); x++)\n            {\n                if (matrix[y][x] < 0)\n                    even = !even;\n                int n = matrix[y][x] < 0 ? -1 * matrix[y][x] : matrix[y][x];\n                m = min(m,n);\n                sum += n;\n            }\n        }\n\n        if (!even)\n            sum -= m * 2;\n\n        return sum;\n    }\n};\n```\n\n# Code Python3\n```python3 []\nclass Solution:\n    def maxMatrixSum(self, matrix: List[List[int]]) -> int:\n        m = 100000\n        even = True\n        sum = 0\n\n        for y in range(len(matrix)):\n            for x in range(len(matrix)):\n                if matrix[y][x] < 0:\n                    even = not even\n                n = abs(matrix[y][x])\n                m = min(m,n)\n                sum += n\n\n        if not even:\n            sum -= m * 2\n\n        return sum\n```\n\n\n\n
['Swift', 'Python', 'C++', 'Python3']