Skip to content

Commit

Permalink
Create main.java
Browse files Browse the repository at this point in the history
  • Loading branch information
Alisherka7 authored Nov 24, 2024
1 parent b9e4b64 commit fba749e
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions 1975. Maximum Matrix Sum/main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
class Solution {

public long maxMatrixSum(int[][] matrix) {
long totalSum = 0;
int minAbsVal = Integer.MAX_VALUE;
int negativeCount = 0;

for (int[] row : matrix) {
for (int val : row) {
totalSum += Math.abs(val);
if (val < 0) {
negativeCount++;
}
minAbsVal = Math.min(minAbsVal, Math.abs(val));
}
}

// Adjust if the count of negative numbers is odd
if (negativeCount % 2 != 0) {
totalSum -= 2 * minAbsVal;
}

return totalSum;
}
}

0 comments on commit fba749e

Please sign in to comment.