Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add solution and test-cases for problem 1975 #1042

Merged
merged 1 commit into from
Dec 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added leetcode/1901-2000/1975.Maximum-Matrix-Sum/1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added leetcode/1901-2000/1975.Maximum-Matrix-Sum/2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
36 changes: 22 additions & 14 deletions leetcode/1901-2000/1975.Maximum-Matrix-Sum/README.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,36 @@
# [1975.Maximum Matrix Sum][title]

> [!WARNING|style:flat]
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)

## Description
You are given an `n x n` integer `matrix`. You can do the following operation **any** number of times:

- Choose any two **adjacent** elements of `matrix` and **multiply** each of them by ``-1`.

Two elements are considered **adjacent** if and only if they share a **border**.

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.

**Example 1:**
**Example 1:**

![1](./1.png)

```
Input: a = "11", b = "1"
Output: "100"
Input: matrix = [[1,-1],[-1,1]]
Output: 4
Explanation: We can follow the following steps to reach sum equals 4:
- Multiply the 2 elements in the first row by -1.
- Multiply the 2 elements in the first column by -1.
```

## 题意
> ...
**Example 2:**

## 题解
![2](./2.png)

### 思路1
> ...
Maximum Matrix Sum
```go
```

Input: matrix = [[1,2,3],[-1,-2,-3],[1,2,3]]
Output: 16
Explanation: We can follow the following step to reach sum equals 16:
- Multiply the 2 last elements in the second row by -1.
```

## 结语

Expand Down
25 changes: 23 additions & 2 deletions leetcode/1901-2000/1975.Maximum-Matrix-Sum/Solution.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,26 @@
package Solution

func Solution(x bool) bool {
return x
func Solution(matrix [][]int) int64 {
ans, neg := int64(0), 0
_min := int64(100001)
zero := 0
rows, cols := len(matrix), len(matrix[0])
for i := range rows {
for j := range cols {
cur := int64(matrix[i][j])
if cur == 0 {
zero++
}
if cur < 0 {
neg++
cur = -cur
}
ans += cur
_min = min(_min, cur)
}
}
if neg&1 == 1 && zero == 0 {
ans -= 2 * _min
}
return ans
}
13 changes: 6 additions & 7 deletions leetcode/1901-2000/1975.Maximum-Matrix-Sum/Solution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,11 @@ func TestSolution(t *testing.T) {
// 测试用例
cases := []struct {
name string
inputs bool
expect bool
inputs [][]int
expect int64
}{
{"TestCase", true, true},
{"TestCase", true, true},
{"TestCase", false, false},
{"TestCase1", [][]int{{1, -1}, {-1, 1}}, 4},
{"TestCase2", [][]int{{1, 2, 3}, {-1, 2, 3}, {1, 2, 3}}, 16},
}

// 开始测试
Expand All @@ -30,10 +29,10 @@ func TestSolution(t *testing.T) {
}
}

// 压力测试
// 压力测试
func BenchmarkSolution(b *testing.B) {
}

// 使用案列
// 使用案列
func ExampleSolution() {
}
Loading