Kotlin | Rust
\n\nhttps://youtu.be/91zS-ylJXMs\n\n#### Join me on Telegram\n\nhttps://t.me/leetcode_daily_unstoppable/810\n\n#### Problem TLDR\n\nMax sum of 2D matrix after m
![1.webp](https://assets.leetcode.com/users/images/678f2833-ace3-4618-bc50-aa13b79f3a35_1732433341.108196.webp)\n\nhttps://youtu.be/91zS-ylJXMs\n\n#### Join me on Telegram\n\nhttps://t.me/leetcode_daily_unstoppable/810\n\n#### Problem TLDR\n\nMax sum of 2D matrix after multiply by -1 adjacent cells #medium\n\n#### Intuition\n\nThis problem is a brainteaser: you must observe how this multiplication by `-1` of adjacent cells works. It works like that:\n* Every negative sign can be moved anywhere\n* Even negative signs all cancels out\n* Odd negative signs leave only a single negative cell\n\nPeek at the smallest value to subtract.\n\n#### Approach\n\n* Imagine the moves, make conclusions\n* Can you brute force the multiplication of cells without these simplifications?\n\n#### Complexity\n\n- Time complexity:\n$$O(n^2)$$\n\n- Space complexity:\n$$O(1)$$\n\n#### Code\n\n```kotlin []\n\n    fun maxMatrixSum(matrix: Array<IntArray>): Long {\n        var cnt = 0; var min = Int.MAX_VALUE\n        return matrix.sumOf { r -> r.sumOf {\n            min = min(min, abs(it))\n            if (it < 0) cnt++\n            abs(it).toLong()\n        }} - 2 * min * (cnt and 1)\n    }\n\n```\n```rust []\n\n    pub fn max_matrix_sum(matrix: Vec<Vec<i32>>) -> i64 {\n        let (mut cnt, mut min) = (0, i64::MAX);\n        matrix.iter().map(|r| \n            r.iter().map(|&v| {\n                let a = v.abs() as i64;\n                min = min.min(a); if (v < 0) { cnt += 1 }; a\n            }).sum::<i64>()\n        ).sum::<i64>() - 2 * min * (cnt & 1)\n    }\n\n```\n```c++ []\n\n    long long maxMatrixSum(vector<vector<int>>& matrix) {\n        int cnt = 0, m = INT_MAX; long long res = 0;\n        for (int y = 0; y < matrix.size(); ++y)\n            for (int x = 0; x < matrix[0].size(); ++x) {\n                m = min(m, abs(matrix[y][x]));\n                if (matrix[y][x] < 0) cnt++;\n                res += abs(matrix[y][x]);\n            }\n        return res - 2 * m * (cnt & 1);\n    }\n\n```
['Brainteaser', 'Matrix', 'C++', 'Rust', 'Kotlin']