Skip to content

Commit 537f521

Browse files
committed
feat: add solutions to lc problem: No.0861.Score After Flipping Matrix
1 parent 23b2f70 commit 537f521

File tree

6 files changed

+273
-25
lines changed

6 files changed

+273
-25
lines changed

solution/0800-0899/0861.Score After Flipping Matrix/README.md

Lines changed: 98 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,27 +37,123 @@
3737
<li><code>A[i][j]</code>&nbsp;是&nbsp;<code>0</code> 或&nbsp;<code>1</code></li>
3838
</ol>
3939

40-
4140
## 解法
4241

4342
<!-- 这里可写通用的实现逻辑 -->
4443

44+
贪心。
45+
46+
每一行的数字要尽可能大,因此,遍历每一行,若行首元素为 0,则将该行每个元素进行翻转,即 `grid[i][j] ^= 1`
47+
48+
接着,遍历每一列,统计列中元素为 1 的个数 `cnt`,若 `cnt`(1 的个数) 比 `m - cnt`(0 的个数) 小,则将该列进行翻转。实际过程中,并不需要对列进行翻转,只需要取 `max(cnt, m - cnt)`,即表示 1 的个数,再乘上该位的大小 `n - j - 1`,即求得当前列的大小。累加每一列大小即可。
49+
4550
<!-- tabs:start -->
4651

4752
### **Python3**
4853

4954
<!-- 这里可写当前语言的特殊实现逻辑 -->
5055

5156
```python
52-
57+
class Solution:
58+
def matrixScore(self, grid: List[List[int]]) -> int:
59+
m, n = len(grid), len(grid[0])
60+
for i in range(m):
61+
if grid[i][0] == 0:
62+
for j in range(n):
63+
grid[i][j] ^= 1
64+
65+
res = 0
66+
for j in range(n):
67+
cnt = 0
68+
for i in range(m):
69+
cnt += grid[i][j]
70+
res += max(cnt, m - cnt) * (1 << (n - j - 1))
71+
return res
5372
```
5473

5574
### **Java**
5675

5776
<!-- 这里可写当前语言的特殊实现逻辑 -->
5877

5978
```java
79+
class Solution {
80+
public int matrixScore(int[][] grid) {
81+
int m = grid.length, n = grid[0].length;
82+
for (int i = 0; i < m; ++i) {
83+
if (grid[i][0] == 0) {
84+
for (int j = 0; j < n; ++j) {
85+
grid[i][j] ^= 1;
86+
}
87+
}
88+
}
89+
int res = 0;
90+
for (int j = 0; j < n; ++j) {
91+
int cnt = 0;
92+
for (int i = 0; i < m; ++i) {
93+
cnt += grid[i][j];
94+
}
95+
res += Math.max(cnt, m - cnt) * (1 << (n - j - 1));
96+
}
97+
return res;
98+
}
99+
}
100+
```
101+
102+
### **C++**
103+
104+
```cpp
105+
class Solution {
106+
public:
107+
int matrixScore(vector<vector<int>>& grid) {
108+
int m = grid.size(), n = grid[0].size();
109+
for (int i = 0; i < m; ++i)
110+
{
111+
if (grid[i][0] == 0)
112+
{
113+
for (int j = 0; j < n; ++j) grid[i][j] ^= 1;
114+
}
115+
}
116+
int res = 0;
117+
for (int j = 0; j < n; ++j)
118+
{
119+
int cnt = 0;
120+
for (int i = 0; i < m; ++i) cnt += grid[i][j];
121+
res += max(cnt, m - cnt) * (1 << (n - j - 1));
122+
}
123+
return res;
124+
}
125+
};
126+
```
60127
128+
### **Go**
129+
130+
```go
131+
func matrixScore(grid [][]int) int {
132+
m, n := len(grid), len(grid[0])
133+
for i := 0; i < m; i++ {
134+
if grid[i][0] == 0 {
135+
for j := 0; j < n; j++ {
136+
grid[i][j] ^= 1
137+
}
138+
}
139+
}
140+
res := 0
141+
for j := 0; j < n; j++ {
142+
cnt := 0
143+
for i := 0; i < m; i++ {
144+
cnt += grid[i][j]
145+
}
146+
res += max(cnt, m-cnt) * (1 << (n - j - 1))
147+
}
148+
return res
149+
}
150+
151+
func max(a, b int) int {
152+
if a > b {
153+
return a
154+
}
155+
return b
156+
}
61157
```
62158

63159
### **...**

solution/0800-0899/0861.Score After Flipping Matrix/README_EN.md

Lines changed: 92 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,36 +6,22 @@
66

77
<p>We have a two dimensional matrix&nbsp;<code>A</code> where each value is <code>0</code> or <code>1</code>.</p>
88

9-
10-
119
<p>A move consists of choosing any row or column, and toggling each value in that row or column: changing all <code>0</code>s to <code>1</code>s, and all <code>1</code>s to <code>0</code>s.</p>
1210

13-
14-
1511
<p>After making any number of moves, every row of this matrix is interpreted as a binary number, and the score of the matrix is the sum of these numbers.</p>
1612

17-
18-
1913
<p>Return the highest possible&nbsp;score.</p>
2014

21-
22-
2315
<p>&nbsp;</p>
2416

25-
26-
2717
<ol>
2818

2919
</ol>
3020

31-
32-
3321
<div>
3422

3523
<p><strong>Example 1:</strong></p>
3624

37-
38-
3925
<pre>
4026

4127
<strong>Input: </strong><span id="example-input-1-1">[[0,0,1,1],[1,0,1,0],[1,1,0,0]]</span>
@@ -48,16 +34,10 @@
4834

4935
0b1111 + 0b1001 + 0b1111 = 15 + 9 + 15 = 39</span></pre>
5036

51-
52-
5337
<p>&nbsp;</p>
5438

55-
56-
5739
<p><strong>Note:</strong></p>
5840

59-
60-
6141
<ol>
6242
<li><code>1 &lt;= A.length &lt;= 20</code></li>
6343
<li><code>1 &lt;= A[0].length &lt;= 20</code></li>
@@ -66,22 +46,111 @@
6646

6747
</div>
6848

69-
70-
7149
## Solutions
7250

7351
<!-- tabs:start -->
7452

7553
### **Python3**
7654

7755
```python
78-
56+
class Solution:
57+
def matrixScore(self, grid: List[List[int]]) -> int:
58+
m, n = len(grid), len(grid[0])
59+
for i in range(m):
60+
if grid[i][0] == 0:
61+
for j in range(n):
62+
grid[i][j] ^= 1
63+
64+
res = 0
65+
for j in range(n):
66+
cnt = 0
67+
for i in range(m):
68+
cnt += grid[i][j]
69+
res += max(cnt, m - cnt) * (1 << (n - j - 1))
70+
return res
7971
```
8072

8173
### **Java**
8274

8375
```java
76+
class Solution {
77+
public int matrixScore(int[][] grid) {
78+
int m = grid.length, n = grid[0].length;
79+
for (int i = 0; i < m; ++i) {
80+
if (grid[i][0] == 0) {
81+
for (int j = 0; j < n; ++j) {
82+
grid[i][j] ^= 1;
83+
}
84+
}
85+
}
86+
int res = 0;
87+
for (int j = 0; j < n; ++j) {
88+
int cnt = 0;
89+
for (int i = 0; i < m; ++i) {
90+
cnt += grid[i][j];
91+
}
92+
res += Math.max(cnt, m - cnt) * (1 << (n - j - 1));
93+
}
94+
return res;
95+
}
96+
}
97+
```
98+
99+
### **C++**
100+
101+
```cpp
102+
class Solution {
103+
public:
104+
int matrixScore(vector<vector<int>>& grid) {
105+
int m = grid.size(), n = grid[0].size();
106+
for (int i = 0; i < m; ++i)
107+
{
108+
if (grid[i][0] == 0)
109+
{
110+
for (int j = 0; j < n; ++j) grid[i][j] ^= 1;
111+
}
112+
}
113+
int res = 0;
114+
for (int j = 0; j < n; ++j)
115+
{
116+
int cnt = 0;
117+
for (int i = 0; i < m; ++i) cnt += grid[i][j];
118+
res += max(cnt, m - cnt) * (1 << (n - j - 1));
119+
}
120+
return res;
121+
}
122+
};
123+
```
84124
125+
### **Go**
126+
127+
```go
128+
func matrixScore(grid [][]int) int {
129+
m, n := len(grid), len(grid[0])
130+
for i := 0; i < m; i++ {
131+
if grid[i][0] == 0 {
132+
for j := 0; j < n; j++ {
133+
grid[i][j] ^= 1
134+
}
135+
}
136+
}
137+
res := 0
138+
for j := 0; j < n; j++ {
139+
cnt := 0
140+
for i := 0; i < m; i++ {
141+
cnt += grid[i][j]
142+
}
143+
res += max(cnt, m-cnt) * (1 << (n - j - 1))
144+
}
145+
return res
146+
}
147+
148+
func max(a, b int) int {
149+
if a > b {
150+
return a
151+
}
152+
return b
153+
}
85154
```
86155

87156
### **...**
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution {
2+
public:
3+
int matrixScore(vector<vector<int>>& grid) {
4+
int m = grid.size(), n = grid[0].size();
5+
for (int i = 0; i < m; ++i)
6+
{
7+
if (grid[i][0] == 0)
8+
{
9+
for (int j = 0; j < n; ++j) grid[i][j] ^= 1;
10+
}
11+
}
12+
int res = 0;
13+
for (int j = 0; j < n; ++j)
14+
{
15+
int cnt = 0;
16+
for (int i = 0; i < m; ++i) cnt += grid[i][j];
17+
res += max(cnt, m - cnt) * (1 << (n - j - 1));
18+
}
19+
return res;
20+
}
21+
};
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
func matrixScore(grid [][]int) int {
2+
m, n := len(grid), len(grid[0])
3+
for i := 0; i < m; i++ {
4+
if grid[i][0] == 0 {
5+
for j := 0; j < n; j++ {
6+
grid[i][j] ^= 1
7+
}
8+
}
9+
}
10+
res := 0
11+
for j := 0; j < n; j++ {
12+
cnt := 0
13+
for i := 0; i < m; i++ {
14+
cnt += grid[i][j]
15+
}
16+
res += max(cnt, m-cnt) * (1 << (n - j - 1))
17+
}
18+
return res
19+
}
20+
21+
func max(a, b int) int {
22+
if a > b {
23+
return a
24+
}
25+
return b
26+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution {
2+
public int matrixScore(int[][] grid) {
3+
int m = grid.length, n = grid[0].length;
4+
for (int i = 0; i < m; ++i) {
5+
if (grid[i][0] == 0) {
6+
for (int j = 0; j < n; ++j) {
7+
grid[i][j] ^= 1;
8+
}
9+
}
10+
}
11+
int res = 0;
12+
for (int j = 0; j < n; ++j) {
13+
int cnt = 0;
14+
for (int i = 0; i < m; ++i) {
15+
cnt += grid[i][j];
16+
}
17+
res += Math.max(cnt, m - cnt) * (1 << (n - j - 1));
18+
}
19+
return res;
20+
}
21+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution:
2+
def matrixScore(self, grid: List[List[int]]) -> int:
3+
m, n = len(grid), len(grid[0])
4+
for i in range(m):
5+
if grid[i][0] == 0:
6+
for j in range(n):
7+
grid[i][j] ^= 1
8+
9+
res = 0
10+
for j in range(n):
11+
cnt = 0
12+
for i in range(m):
13+
cnt += grid[i][j]
14+
res += max(cnt, m - cnt) * (1 << (n - j - 1))
15+
return res

0 commit comments

Comments
 (0)