bumpsoo 2024-11-24 20:14:03 +09:00
parent 1d473fe723
commit 7eb3891f7a

View file

@ -0,0 +1,20 @@
# https://leetcode.com/problems/maximum-matrix-sum
from typing import List
class Solution:
def maxMatrixSum(self, matrix: List[List[int]]) -> int:
total = 0
negative_count = 0
min_val = float("inf")
length = len(matrix)
for i in range(length):
for j in range(length):
if matrix[i][j] < 0:
total += -matrix[i][j]
negative_count += 1
else:
total += matrix[i][j]
min_val = min(min_val, abs(matrix[i][j]))
if negative_count % 2 != 0:
total -= 2 * int(min_val)
return total