This commit is contained in:
parent
1d473fe723
commit
7eb3891f7a
1 changed files with 20 additions and 0 deletions
20
medium/maximum_matrix_sum.py
Normal file
20
medium/maximum_matrix_sum.py
Normal 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
|
||||
Loading…
Add table
Add a link
Reference in a new issue