Skip to content

Commit 829a254

Browse files
committed
Add solution and test-cases for problem 1975
1 parent dbd4dda commit 829a254

File tree

5 files changed

+51
-23
lines changed

5 files changed

+51
-23
lines changed
3.97 KB
Loading
6.73 KB
Loading

leetcode/1901-2000/1975.Maximum-Matrix-Sum/README.md

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,36 @@
11
# [1975.Maximum Matrix Sum][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
4+
You are given an `n x n` integer `matrix`. You can do the following operation **any** number of times:
5+
6+
- Choose any two **adjacent** elements of `matrix` and **multiply** each of them by ``-1`.
7+
8+
Two elements are considered **adjacent** if and only if they share a **border**.
9+
10+
Your goal is to **maximum** the summation of the matrix's elements. Return the **maximum** sum of the matrix's elements using the operation mentioned above.
711

8-
**Example 1:**
12+
**Example 1:**
13+
14+
![1](./1.png)
915

1016
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
17+
Input: matrix = [[1,-1],[-1,1]]
18+
Output: 4
19+
Explanation: We can follow the following steps to reach sum equals 4:
20+
- Multiply the 2 elements in the first row by -1.
21+
- Multiply the 2 elements in the first column by -1.
1322
```
1423

15-
## 题意
16-
> ...
24+
**Example 2:**
1725

18-
## 题解
26+
![2](./2.png)
1927

20-
### 思路1
21-
> ...
22-
Maximum Matrix Sum
23-
```go
2428
```
25-
29+
Input: matrix = [[1,2,3],[-1,-2,-3],[1,2,3]]
30+
Output: 16
31+
Explanation: We can follow the following step to reach sum equals 16:
32+
- Multiply the 2 last elements in the second row by -1.
33+
```
2634

2735
## 结语
2836

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,26 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(matrix [][]int) int64 {
4+
ans, neg := int64(0), 0
5+
_min := int64(100001)
6+
zero := 0
7+
rows, cols := len(matrix), len(matrix[0])
8+
for i := range rows {
9+
for j := range cols {
10+
cur := int64(matrix[i][j])
11+
if cur == 0 {
12+
zero++
13+
}
14+
if cur < 0 {
15+
neg++
16+
cur = -cur
17+
}
18+
ans += cur
19+
_min = min(_min, cur)
20+
}
21+
}
22+
if neg&1 == 1 && zero == 0 {
23+
ans -= 2 * _min
24+
}
25+
return ans
526
}

leetcode/1901-2000/1975.Maximum-Matrix-Sum/Solution_test.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,11 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
inputs [][]int
14+
expect int64
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", [][]int{{1, -1}, {-1, 1}}, 4},
17+
{"TestCase2", [][]int{{1, 2, 3}, {-1, 2, 3}, {1, 2, 3}}, 16},
1918
}
2019

2120
// 开始测试
@@ -30,10 +29,10 @@ func TestSolution(t *testing.T) {
3029
}
3130
}
3231

33-
// 压力测试
32+
// 压力测试
3433
func BenchmarkSolution(b *testing.B) {
3534
}
3635

37-
// 使用案列
36+
// 使用案列
3837
func ExampleSolution() {
3938
}

0 commit comments

Comments
 (0)