diff --git a/leetcode/1901-2000/1975.Maximum-Matrix-Sum/1.png b/leetcode/1901-2000/1975.Maximum-Matrix-Sum/1.png new file mode 100644 index 000000000..7da49b04b Binary files /dev/null and b/leetcode/1901-2000/1975.Maximum-Matrix-Sum/1.png differ diff --git a/leetcode/1901-2000/1975.Maximum-Matrix-Sum/2.png b/leetcode/1901-2000/1975.Maximum-Matrix-Sum/2.png new file mode 100644 index 000000000..76575375f Binary files /dev/null and b/leetcode/1901-2000/1975.Maximum-Matrix-Sum/2.png differ diff --git a/leetcode/1901-2000/1975.Maximum-Matrix-Sum/README.md b/leetcode/1901-2000/1975.Maximum-Matrix-Sum/README.md index 81bdefe9c..fa22eb4a4 100755 --- a/leetcode/1901-2000/1975.Maximum-Matrix-Sum/README.md +++ b/leetcode/1901-2000/1975.Maximum-Matrix-Sum/README.md @@ -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. +``` ## 结语 diff --git a/leetcode/1901-2000/1975.Maximum-Matrix-Sum/Solution.go b/leetcode/1901-2000/1975.Maximum-Matrix-Sum/Solution.go index d115ccf5e..4159fb577 100644 --- a/leetcode/1901-2000/1975.Maximum-Matrix-Sum/Solution.go +++ b/leetcode/1901-2000/1975.Maximum-Matrix-Sum/Solution.go @@ -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 } diff --git a/leetcode/1901-2000/1975.Maximum-Matrix-Sum/Solution_test.go b/leetcode/1901-2000/1975.Maximum-Matrix-Sum/Solution_test.go index 14ff50eb4..463da125f 100644 --- a/leetcode/1901-2000/1975.Maximum-Matrix-Sum/Solution_test.go +++ b/leetcode/1901-2000/1975.Maximum-Matrix-Sum/Solution_test.go @@ -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}, } // 开始测试 @@ -30,10 +29,10 @@ func TestSolution(t *testing.T) { } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }