#1975
Maximum Matrix Sum
pupil · 595 · lc medium +29 · verified · 67.6% accepted · 1,617 likes · top 74%
Description
Given an n x n integer matrix, you may repeat the following operation any number of times: pick two bordering elements and negate both by multiplying each by -1.
Maximize the total sum of all elements in the matrix and return that maximum sum.
Example 1:
Input: matrix = [[1,-1],[-1,1]]
Output: 4
Explanation: We can follow the following steps to reach sum equals 4:
- Multiply the 2 elements in the first row by -1.
- Multiply the 2 elements in the first column by -1.
Example 2:
Input: matrix = [[1,2,3],[-1,-2,-3],[1,2,3]]
Output: 16
Explanation: We can follow the following step to reach sum equals 16:
- Multiply the 2 last elements in the second row by -1.
Code
1
2
3