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 861 #879

Merged
merged 1 commit into from
Jun 6, 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
32 changes: 17 additions & 15 deletions leetcode/801-900/0861.Score-After-Flipping-Matrix/README.md
Original file line number Diff line number Diff line change
@@ -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
```

## 结语

Expand Down
34 changes: 32 additions & 2 deletions leetcode/801-900/0861.Score-After-Flipping-Matrix/Solution.go
Original file line number Diff line number Diff line change
@@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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},
}

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

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

// 使用案列
// 使用案列
func ExampleSolution() {
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading