↓Skip to main content
  1. LeetCode/

LeetCode 1975: Maximum Matrix Sum

·2 mins· ·
LeetCode Medium Greedy Matrix Array
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: medium First Attempt: 2026-01-06

  • Submissions: Accepted (first try)

Approach
#

Multiplying two adjacent cells by -1:

  • With one negative next to a positive, the minus sign “moves” next door. Repeating along a path, a minus sign can reach any cell.
  • Two minus signs meeting cancel at once.
  • Every operation flips two cells, so the parity of the negative count never changes (a 0 can absorb a sign).

So with an even count everything becomes positive and the answer is the sum of absolute values; with an odd count one negative must remain, placed on the smallest absolute value, giving sum - 2 × smallest. A 0 makes the smallest 0, which handles itself.

My Solution
#

class Solution:
    def maxMatrixSum(self, matrix: List[List[int]]) -> int:
        total, smallest, neg = 0, inf, 0
        for row in matrix:
            for x in row:
                total += abs(x)
                smallest = min(smallest, abs(x))
                neg += x < 0
        return total if neg % 2 == 0 else total - 2 * smallest

I originally kept a separate zero flag; with a 0, smallest is 0 and subtracting it changes nothing, so it is unnecessary.

Complexity
#

Time O(n²), space O(1).

Mistakes to Avoid
#

  • The remaining minus goes on the smallest absolute value, not the smallest value.

Interview Explanation
#

Operations can carry a minus sign anywhere along a path and two signs cancel when they meet, but the parity of the negative count is fixed. So an even count gives the sum of absolute values, and an odd count subtracts twice the smallest absolute value. Time O(n²).

Related

LeetCode 3796: Find Maximum Value in a Constrained Sequence
·2 mins
LeetCode Medium Greedy Array
LeetCode 3796 note: each position’s bound is the min of its own limit, the left bound + difference and the right bound + difference; one pass each way
LeetCode 3689: Maximum Total Subarray Value I
·1 min
LeetCode Medium Greedy Array
LeetCode 3689 note: the same subarray may be chosen repeatedly, and the whole array’s max minus min is the best single value, so choose it k times
LeetCode 2294: Partition Array Such That Maximum Difference Is K
·1 min
LeetCode Medium Greedy Sorting Array
LeetCode 2294 note: subsequences ignore order, so sort and extend each group from its minimum until a value exceeds min + k
LeetCode 3195: Find the Minimum Area to Cover All Ones I
·1 min
LeetCode Medium Array Matrix
LeetCode 3195 note: track the topmost, bottommost, leftmost and rightmost 1; the area is height times width
LeetCode 1222: Queens That Can Attack the King
·2 mins
LeetCode Medium Array Matrix Simulation
LeetCode 1222 note: step outward from the king in eight directions; the first queen met in each direction can attack, with queens in a set for lookups
LeetCode 2410: Maximum Matching of Players With Trainers
·2 mins
LeetCode Medium Two-Pointers Greedy Sorting Array
LeetCode 2410 note: sort both sides, then same-direction pointers match the weakest player with the smallest trainer that is good enough