Solution 1

Did not go through the solution yet!

Python · 2026-01-05
class Solution:
    '''
    Did not go through the solution yet!
    '''
    def maxMatrixSum(self, matrix: List[List[int]]) -> int:
        total_sum = 0
        min_abs = float('inf')
        negative_count = 0
        
        for row in matrix:
            for val in row:
                if val < 0:
                    negative_count += 1
                abs_val = abs(val)
                total_sum += abs_val
                min_abs = min(min_abs, abs_val)
        
        # If number of negatives is odd, one smallest absolute value must stay negative
        if negative_count % 2 == 1:
            total_sum -= 2 * min_abs
        
        return total_sum
Leet Code/python.py · L3792–3814