1975. Maximum Matrix Sum Medium

@problem@discussion
#Array#Greedy#Matrix



1/**
2 * [1975] Maximum Matrix Sum
3 *
4 * You are given an n x n integer matrix. You can do the following operation any number of times:
5 * 
6 * 	Choose any two adjacent elements of matrix and multiply each of them by -1.
7 * 
8 * Two elements are considered adjacent if and only if they share a border.
9 * 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.
10 *  
11 * Example 1:
12 * <img alt="" src="https://assets.leetcode.com/uploads/2021/07/16/pc79-q2ex1.png" style="width: 401px; height: 81px;" />
13 * Input: matrix = [[1,-1],[-1,1]]
14 * Output: 4
15 * Explanation: We can follow the following steps to reach sum equals 4:
16 * - Multiply the 2 elements in the first row by -1.
17 * - Multiply the 2 elements in the first column by -1.
18 * 
19 * Example 2:
20 * <img alt="" src="https://assets.leetcode.com/uploads/2021/07/16/pc79-q2ex2.png" style="width: 321px; height: 121px;" />
21 * Input: matrix = [[1,2,3],[-1,-2,-3],[1,2,3]]
22 * Output: 16
23 * Explanation: We can follow the following step to reach sum equals 16:
24 * - Multiply the 2 last elements in the second row by -1.
25 * 
26 *  
27 * Constraints:
28 * 
29 * 	n == matrix.length == matrix[i].length
30 * 	2 <= n <= 250
31 * 	-10^5 <= matrix[i][j] <= 10^5
32 * 
33 */
34pub struct Solution {}
35
36// problem: https://leetcode.com/problems/maximum-matrix-sum/
37// discuss: https://leetcode.com/problems/maximum-matrix-sum/discuss/?currentPage=1&orderBy=most_votes&query=
38
39// submission codes start here
40
41impl Solution {
42    pub fn max_matrix_sum(matrix: Vec<Vec<i32>>) -> i64 {
43        
44    }
45}
46
47// submission codes end
48
49#[cfg(test)]
50mod tests {
51    use super::*;
52
53    #[test]
54    fn test_1975() {
55    }
56}
57


Back
© 2025 bowen.ge All Rights Reserved.