-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1975_Maximum_Matrix_Sum.py
More file actions
27 lines (26 loc) · 891 Bytes
/
1975_Maximum_Matrix_Sum.py
File metadata and controls
27 lines (26 loc) · 891 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
class Solution:
def maxMatrixSum(self, matrix: List[List[int]]) -> int:
sol = 0
max_negative = float('-inf')
max_negative_num = 0
min_positive = float('inf')
isZero = False
for each_m in matrix:
for each_n in each_m:
if each_n < 0:
max_negative_num += 1
sol += -1 * each_n
if max_negative < each_n:
max_negative = each_n
elif each_n == 0:
isZero = True
else:
if min_positive > each_n:
min_positive = each_n
sol += each_n
if isZero:
return sol
elif max_negative_num % 2 == 1:
return max(sol + max_negative * 2, sol - min_positive * 2)
else:
return sol