Skip to content

Commit 94e154a

Browse files
committed
Add solution and test-cases for problem 3239
1 parent df4288a commit 94e154a

File tree

5 files changed

+67
-22
lines changed

5 files changed

+67
-22
lines changed
5.07 KB
Loading
4.55 KB
Loading

leetcode/3201-3300/3239.Minimum-Number-of-Flips-to-Make-Binary-Grid-Palindromic-I/README.md

Lines changed: 39 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,54 @@
11
# [3239.Minimum Number of Flips to Make Binary Grid Palindromic I][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`.
5+
6+
A row or column is considered `palindromic` if its values read the same forward and backward.
7+
8+
You can **flip** any number of cells in `grid` from `0` to `1`, or from `1` to `0`.
9+
10+
Return the **minimum** number of cells that need to be flipped to make **either** all rows **palindromic** or all columns **palindromic**.
11+
712

8-
**Example 1:**
13+
**Example 1:**
14+
15+
![1](./1.png)
916

1017
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
18+
Input: grid = [[1,0,0],[0,0,0],[0,0,1]]
19+
20+
Output: 2
21+
22+
Explanation:
23+
24+
Flipping the highlighted cells makes all the rows palindromic.
1325
```
1426

15-
## 题意
16-
> ...
27+
**Example 2:**
1728

18-
## 题解
29+
![2](./2.png)
1930

20-
### 思路1
21-
> ...
22-
Minimum Number of Flips to Make Binary Grid Palindromic I
23-
```go
2431
```
32+
Input: grid = [[0,1],[0,1],[0,0]]
33+
34+
Output: 1
2535
36+
Explanation:
37+
38+
Flipping the highlighted cell makes all the columns palindromic.
39+
```
40+
41+
**Example 3:**
42+
43+
```
44+
Input: grid = [[1],[0]]
45+
46+
Output: 0
47+
48+
Explanation:
49+
50+
All rows are already palindromic.
51+
```
2652

2753
## 结语
2854

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,24 @@
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+
ans := rows * cols
6+
need := 0
7+
for i := 0; i < rows; i++ {
8+
for s, e := 0, cols-1; s < e; s, e = s+1, e-1 {
9+
if grid[i][s] != grid[i][e] {
10+
need++
11+
}
12+
}
13+
}
14+
ans = min(ans, need)
15+
need = 0
16+
for i := 0; i < cols; i++ {
17+
for s, e := 0, rows-1; s < e; s, e = s+1, e-1 {
18+
if grid[s][i] != grid[e][i] {
19+
need++
20+
}
21+
}
22+
}
23+
return min(ans, need)
524
}

leetcode/3201-3300/3239.Minimum-Number-of-Flips-to-Make-Binary-Grid-Palindromic-I/Solution_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ 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{{1, 0, 0}, {0, 0, 0}, {0, 0, 1}}, 2},
17+
{"TestCase2", [][]int{{0, 1}, {0, 1}, {0, 0}}, 1},
18+
{"TestCase3", [][]int{{1}, {0}}, 0},
1919
}
2020

2121
// 开始测试
@@ -30,10 +30,10 @@ func TestSolution(t *testing.T) {
3030
}
3131
}
3232

33-
// 压力测试
33+
// 压力测试
3434
func BenchmarkSolution(b *testing.B) {
3535
}
3636

37-
// 使用案列
37+
// 使用案列
3838
func ExampleSolution() {
3939
}

0 commit comments

Comments
 (0)