diff --git a/leetcode/801-900/0861.Score-After-Flipping-Matrix/README.md b/leetcode/801-900/0861.Score-After-Flipping-Matrix/README.md index 3d52a7918..64fdcb051 100644 --- a/leetcode/801-900/0861.Score-After-Flipping-Matrix/README.md +++ b/leetcode/801-900/0861.Score-After-Flipping-Matrix/README.md @@ -1,28 +1,30 @@ # [861.Score After Flipping Matrix][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 `m x n` binary matrix `grid`. -**Example 1:** +A **move** consists of choosing any row or column and toggling each value in that row or column (i.e., changing all `0`'s to `1`'s, and all `1`'s to `0`'s). -``` -Input: a = "11", b = "1" -Output: "100" -``` +Every row of the matrix is interpreted as a binary number, and the **score** of the matrix is the sum of these numbers. + +Return the highest possible **score** after making any number of **moves** (including zero moves). -## 题意 -> ... +**Example 1:** -## 题解 +![1](./lc-toogle1.jpeg) -### 思路1 -> ... -Score After Flipping Matrix -```go ``` +Input: grid = [[0,0,1,1],[1,0,1,0],[1,1,0,0]] +Output: 39 +Explanation: 0b1111 + 0b1001 + 0b1111 = 15 + 9 + 15 = 39 +``` + +**Example 2:** +``` +Input: grid = [[0]] +Output: 1 +``` ## 结语 diff --git a/leetcode/801-900/0861.Score-After-Flipping-Matrix/Solution.go b/leetcode/801-900/0861.Score-After-Flipping-Matrix/Solution.go index d115ccf5e..ecaecef88 100644 --- a/leetcode/801-900/0861.Score-After-Flipping-Matrix/Solution.go +++ b/leetcode/801-900/0861.Score-After-Flipping-Matrix/Solution.go @@ -1,5 +1,35 @@ package Solution -func Solution(x bool) bool { - return x +func Solution(grid [][]int) int { + rows, cols := len(grid), len(grid[0]) + for i := 0; i < rows; i++ { + if grid[i][0] == 0 { + for j := 0; j < cols; j++ { + grid[i][j] = 1 - grid[i][j] + } + } + } + for c := 1; c < cols; c++ { + ones := 0 + for r := 0; r < rows; r++ { + if grid[r][c] == 1 { + ones++ + } + } + if ones < rows-ones { + for r := 0; r < rows; r++ { + grid[r][c] = 1 - grid[r][c] + } + } + } + + ans := 0 + for i := 0; i < rows; i++ { + cur := 0 + for j := 0; j < cols; j++ { + cur = cur*2 + grid[i][j] + } + ans += cur + } + return ans } diff --git a/leetcode/801-900/0861.Score-After-Flipping-Matrix/Solution_test.go b/leetcode/801-900/0861.Score-After-Flipping-Matrix/Solution_test.go index 14ff50eb4..1339c5a48 100644 --- a/leetcode/801-900/0861.Score-After-Flipping-Matrix/Solution_test.go +++ b/leetcode/801-900/0861.Score-After-Flipping-Matrix/Solution_test.go @@ -10,12 +10,13 @@ func TestSolution(t *testing.T) { // 测试用例 cases := []struct { name string - inputs bool - expect bool + inputs [][]int + expect int }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, + {"TestCase1", [][]int{ + {0, 0, 1, 1}, {1, 0, 1, 0}, {1, 1, 0, 0}, + }, 39}, + {"TestCase2", [][]int{{0}}, 1}, } // 开始测试 @@ -30,10 +31,10 @@ func TestSolution(t *testing.T) { } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { } diff --git a/leetcode/801-900/0861.Score-After-Flipping-Matrix/lc-toogle1.jpeg b/leetcode/801-900/0861.Score-After-Flipping-Matrix/lc-toogle1.jpeg new file mode 100644 index 000000000..ac1099bfa Binary files /dev/null and b/leetcode/801-900/0861.Score-After-Flipping-Matrix/lc-toogle1.jpeg differ