Skip to content

Commit e0f8926

Browse files
authored
Merge pull request #879 from 0xff-dev/861
Add solution and test-cases for problem 861
2 parents a68ee71 + e5621ec commit e0f8926

File tree

4 files changed

+57
-24
lines changed

4 files changed

+57
-24
lines changed

leetcode/801-900/0861.Score-After-Flipping-Matrix/README.md

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,30 @@
11
# [861.Score After Flipping Matrix][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 `m x n` binary matrix `grid`.
75

8-
**Example 1:**
6+
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).
97

10-
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
13-
```
8+
Every row of the matrix is interpreted as a binary number, and the **score** of the matrix is the sum of these numbers.
9+
10+
Return the highest possible **score** after making any number of **moves** (including zero moves).
1411

15-
## 题意
16-
> ...
12+
**Example 1:**
1713

18-
## 题解
14+
![1](./lc-toogle1.jpeg)
1915

20-
### 思路1
21-
> ...
22-
Score After Flipping Matrix
23-
```go
2416
```
17+
Input: grid = [[0,0,1,1],[1,0,1,0],[1,1,0,0]]
18+
Output: 39
19+
Explanation: 0b1111 + 0b1001 + 0b1111 = 15 + 9 + 15 = 39
20+
```
21+
22+
**Example 2:**
2523

24+
```
25+
Input: grid = [[0]]
26+
Output: 1
27+
```
2628

2729
## 结语
2830

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

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(grid [][]int) int {
4+
rows, cols := len(grid), len(grid[0])
5+
for i := 0; i < rows; i++ {
6+
if grid[i][0] == 0 {
7+
for j := 0; j < cols; j++ {
8+
grid[i][j] = 1 - grid[i][j]
9+
}
10+
}
11+
}
12+
for c := 1; c < cols; c++ {
13+
ones := 0
14+
for r := 0; r < rows; r++ {
15+
if grid[r][c] == 1 {
16+
ones++
17+
}
18+
}
19+
if ones < rows-ones {
20+
for r := 0; r < rows; r++ {
21+
grid[r][c] = 1 - grid[r][c]
22+
}
23+
}
24+
}
25+
26+
ans := 0
27+
for i := 0; i < rows; i++ {
28+
cur := 0
29+
for j := 0; j < cols; j++ {
30+
cur = cur*2 + grid[i][j]
31+
}
32+
ans += cur
33+
}
34+
return ans
535
}

leetcode/801-900/0861.Score-After-Flipping-Matrix/Solution_test.go

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

2122
// 开始测试
@@ -30,10 +31,10 @@ func TestSolution(t *testing.T) {
3031
}
3132
}
3233

33-
// 压力测试
34+
// 压力测试
3435
func BenchmarkSolution(b *testing.B) {
3536
}
3637

37-
// 使用案列
38+
// 使用案列
3839
func ExampleSolution() {
3940
}
Loading

0 commit comments

Comments
 (0)