Skip to content

Commit 00bd0f1

Browse files
chore: add LeetCode daily solution
1 parent 962ede1 commit 00bd0f1

5 files changed

Lines changed: 106 additions & 0 deletions

File tree

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Maximum Matrix Sum (Medium)
2+
3+
**Problem ID:** 1975
4+
**Date:** 2026-01-05
5+
**Link:** https://leetcode.com/problems/maximum-matrix-sum/
6+
7+
## Approach
8+
9+
To solve the problem of maximizing the sum of an n x n integer matrix after performing the specified operation (multiplying two adjacent elements by -1), we can follow these steps:
10+
11+
### Problem Understanding
12+
The key operation allows us to change the sign of two adjacent elements in the matrix. This means that we can effectively flip the sign of pairs of elements, which can help us increase the overall sum of the matrix. The goal is to determine the maximum possible sum after performing any number of such operations.
13+
14+
### Approach
15+
1. **Calculate the Initial Sum**: Start by calculating the initial sum of all elements in the matrix. This will serve as a baseline for our calculations.
16+
17+
2. **Identify Negative Elements**: Count the number of negative elements in the matrix. The presence of negative elements is crucial since they will contribute negatively to the overall sum.
18+
19+
3. **Determine the Minimum Absolute Value**: Find the element with the smallest absolute value in the matrix. This value will be important for deciding how to handle the sign flips.
20+
21+
4. **Evaluate the Effect of Flips**:
22+
- If the count of negative elements is even, we can flip pairs of negatives to make them positive, maximizing the sum without any remaining negatives.
23+
- If the count of negative elements is odd, one negative will remain after all possible flips. In this case, we can flip the smallest absolute value element (which could be either negative or positive) to minimize the negative impact on the sum.
24+
25+
5. **Calculate the Maximum Sum**:
26+
- If there are an even number of negatives, the maximum sum is simply the absolute value of the initial sum.
27+
- If there are an odd number of negatives, the maximum sum can be calculated as:
28+
\[
29+
\text{max\_sum} = \text{initial\_sum} + 2 \times \text{smallest\_absolute\_value}
30+
\]
31+
This accounts for flipping the smallest absolute value element to mitigate the effect of the remaining negative.
32+
33+
### Data Structures
34+
- A simple 2D array (matrix) to store the input values.
35+
- Variables to keep track of the initial sum, count of negative elements, and the smallest absolute value.
36+
37+
### Complexity
38+
- **Time Complexity**: O(n^2) due to the need to iterate through all elements of the n x n matrix to calculate the initial sum, count negatives, and find the minimum absolute value.
39+
- **Space Complexity**: O(1) since we are using a constant amount of extra space regardless of the input size.
40+
41+
### Conclusion
42+
By leveraging the properties of the operations allowed and focusing on the count of negative numbers and the smallest absolute value, we can efficiently compute the maximum possible sum of the matrix after performing the operations. This approach ensures that we handle both even and odd counts of negative elements appropriately to achieve the desired result.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution {
2+
public int maxMatrixSum(int[][] matrix) {
3+
int n = matrix.length;
4+
long totalSum = 0;
5+
int minAbsValue = Integer.MAX_VALUE;
6+
int negativeCount = 0;
7+
8+
for (int i = 0; i < n; i++) {
9+
for (int j = 0; j < n; j++) {
10+
int value = matrix[i][j];
11+
totalSum += Math.abs(value);
12+
if (value < 0) {
13+
negativeCount++;
14+
}
15+
minAbsValue = Math.min(minAbsValue, Math.abs(value));
16+
}
17+
}
18+
19+
if (negativeCount % 2 == 0) {
20+
return (int) totalSum;
21+
} else {
22+
return (int) (totalSum - 2 * minAbsValue);
23+
}
24+
}
25+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
var maxMatrixSum = function(matrix) {
2+
let totalSum = 0;
3+
let minAbsValue = Infinity;
4+
let negativeCount = 0;
5+
6+
for (let row of matrix) {
7+
for (let num of row) {
8+
if (num < 0) {
9+
negativeCount++;
10+
}
11+
totalSum += Math.abs(num);
12+
minAbsValue = Math.min(minAbsValue, Math.abs(num));
13+
}
14+
}
15+
16+
if (negativeCount % 2 === 0) {
17+
return totalSum;
18+
} else {
19+
return totalSum - 2 * minAbsValue;
20+
}
21+
};
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution:
2+
def maxMatrixSum(self, matrix: List[List[int]]) -> int:
3+
total_sum = 0
4+
min_abs_value = float('inf')
5+
negative_count = 0
6+
7+
for row in matrix:
8+
for value in row:
9+
if value < 0:
10+
negative_count += 1
11+
total_sum += abs(value)
12+
min_abs_value = min(min_abs_value, abs(value))
13+
14+
if negative_count % 2 == 0:
15+
return total_sum
16+
else:
17+
return total_sum - 2 * min_abs_value

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,3 +326,4 @@ Through completing the Blind 75 and NeetCode 150, you will have mastered:
326326
- 2026-01-02 — [N-Repeated Element in Size 2N Array](https://leetcode.com/problems/n-repeated-element-in-size-2n-array/) (Easy) → `Easy/2026-01-02-961-N-Repeated-Element-in-Size-2N-Array`
327327
- 2026-01-03 — [Number of Ways to Paint N × 3 Grid](https://leetcode.com/problems/number-of-ways-to-paint-n-3-grid/) (Hard) → `Hard/2026-01-03-1411-Number-of-Ways-to-Paint-N-3-Grid`
328328
- 2026-01-04 — [Four Divisors](https://leetcode.com/problems/four-divisors/) (Medium) → `Medium/2026-01-04-1390-Four-Divisors`
329+
- 2026-01-05 — [Maximum Matrix Sum](https://leetcode.com/problems/maximum-matrix-sum/) (Medium) → `Medium/2026-01-05-1975-Maximum-Matrix-Sum`

0 commit comments

Comments
 (0)