Skip to content

Commit 27a6822

Browse files
committed
一刷861
1 parent fa65963 commit 27a6822

6 files changed

Lines changed: 112 additions & 34 deletions

File tree

README.adoc

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6051,14 +6051,14 @@ TIP: **公众号的微信号是: `jikerizhi`**。__因为众所周知的原因
60516051
//|{doc_base_url}/0860-lemonade-change.adoc[题解]
60526052
//|Easy
60536053
//|
6054-
//
6055-
//|{counter:codes}
6056-
//|{leetcode_base_url}/score-after-flipping-matrix/[861. Score After Flipping Matrix^]
6057-
//|{source_base_url}/_0861_ScoreAfterFlippingMatrix.java[Java]
6058-
//|{doc_base_url}/0861-score-after-flipping-matrix.adoc[题解]
6059-
//|Medium
6060-
//|
6061-
//
6054+
6055+
|{counter:codes}
6056+
|{leetcode_base_url}/score-after-flipping-matrix/[861. Score After Flipping Matrix^]
6057+
|{source_base_url}/_0861_ScoreAfterFlippingMatrix.java[Java]
6058+
|{doc_base_url}/0861-score-after-flipping-matrix.adoc[题解]
6059+
|Medium
6060+
|
6061+
60626062
//|{counter:codes}
60636063
//|{leetcode_base_url}/shortest-subarray-with-sum-at-least-k/[862. Shortest Subarray with Sum at Least K^]
60646064
//|{source_base_url}/_0862_ShortestSubarrayWithSumAtLeastK.java[Java]
Lines changed: 46 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,70 @@
11
[#0861-score-after-flipping-matrix]
2-
= 861. Score After Flipping Matrix
2+
= 861. 翻转矩阵后的得分
33

4-
{leetcode}/problems/score-after-flipping-matrix/[LeetCode - Score After Flipping Matrix^]
4+
https://leetcode.cn/problems/score-after-flipping-matrix/[LeetCode - 861. 翻转矩阵后的得分^]
55

6-
We have a two dimensional matrix `A` where each value is `0` or `1`.
6+
给你一个大小为 `m x n` 的二元矩阵 `grid` ,矩阵中每个元素的值为 `0` `1`
77

8-
A move consists of choosing any row or column, and toggling each value in that row or column: changing all `0`s to `1`s, and all `1`s to `0`s.
8+
一次 *移动* 是指选择任一行或列,并转换该行或列中的每一个值:将所有 `0` 都更改为 `1`,将所有 `1` 都更改为 `0`
99

10-
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.
10+
在做出任意次数的移动后,将该矩阵的每一行都按照二进制数来解释,矩阵的 *得分* 就是这些数字的总和。
1111

12-
Return the highest possible score.
12+
在执行任意次 *移动* 后(含 0 次),返回可能的最高分数。
1313

14-
14+
*示例 1:*
1515

16+
image::images/0861-01.jpg[{image_attr}]
1617

18+
....
19+
输入:grid = [[0,0,1,1],[1,0,1,0],[1,1,0,0]]
20+
输出:39
21+
解释:0b1111 + 0b1001 + 0b1111 = 15 + 9 + 15 = 39
22+
....
1723

24+
*示例 2:*
1825

26+
....
27+
输入:grid = [[0]]
28+
输出:1
29+
....
1930

20-
*Example 1:*
21-
22-
[subs="verbatim,quotes,macros"]
23-
----
24-
*Input:* [[0,0,1,1],[1,0,1,0],[1,1,0,0]]
25-
*Output:* 39
26-
*Explanation:*
27-
Toggled to [[1,1,1,1],[1,0,0,1],[1,1,1,1]].
28-
0b1111 + 0b1001 + 0b1111 = 15 + 9 + 15 = 39
29-
----
30-
31-
32-
33-
*Note:*
31+
*提示:*
3432

35-
36-
. `1 <= A.length <= 20`
37-
. `1 <= A[0].length <= 20`
38-
. `A[i][j]` is `0` or `1`.
33+
* `m == grid.length`
34+
* `n == grid[i].length`
35+
* `1 \<= m, n \<= 20+`
36+
* `grid[i][j]` `0` `1`
3937
4038
4139
40+
== 思路分析
4241

4342

4443
[[src-0861]]
44+
[tabs]
45+
====
46+
一刷::
47+
+
48+
--
4549
[{java_src_attr}]
4650
----
4751
include::{sourcedir}/_0861_ScoreAfterFlippingMatrix.java[tag=answer]
4852
----
53+
--
54+
55+
// 二刷::
56+
// +
57+
// --
58+
// [{java_src_attr}]
59+
// ----
60+
// include::{sourcedir}/_0861_ScoreAfterFlippingMatrix_2.java[tag=answer]
61+
// ----
62+
// --
63+
====
64+
65+
66+
== 参考资料
4967

68+
. https://leetcode.cn/problems/score-after-flipping-matrix/solutions/512802/jiu-ying-suan-tong-su-yi-dong-mei-xiang-fgt9j/[861. 翻转矩阵后的得分 - 就硬算,通俗易懂,一开始没想到位操作^]
69+
. https://leetcode.cn/problems/score-after-flipping-matrix/solutions/512323/fan-zhuan-ju-zhen-tan-xin-xin-lu-li-chen-21h7/[861. 翻转矩阵后的得分 - 翻转矩阵贪心心路历程!^]
70+
. https://leetcode.cn/problems/score-after-flipping-matrix/solutions/511825/fan-zhuan-ju-zhen-hou-de-de-fen-by-leetc-cxma/[861. 翻转矩阵后的得分 - 官方题解^]

docs/images/0861-01.jpg

36.8 KB
Loading

docs/index.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1814,7 +1814,7 @@ include::0859-buddy-strings.adoc[leveloffset=+1]
18141814

18151815
// include::0860-lemonade-change.adoc[leveloffset=+1]
18161816

1817-
// include::0861-score-after-flipping-matrix.adoc[leveloffset=+1]
1817+
include::0861-score-after-flipping-matrix.adoc[leveloffset=+1]
18181818

18191819
// include::0862-shortest-subarray-with-sum-at-least-k.adoc[leveloffset=+1]
18201820

logbook/202601.adoc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1152,6 +1152,11 @@ endif::[]
11521152
|{doc_base_url}/0859-buddy-strings.adoc[题解]
11531153
|✅ 哈希。注意处理字符串相同的情况。
11541154

1155+
|{counter:codes}
1156+
|{leetcode_base_url}/score-after-flipping-matrix/[861. Score After Flipping Matrix^]
1157+
|{doc_base_url}/0861-score-after-flipping-matrix.adoc[题解]
1158+
|✅ 贪心。确保最高位为 `1`,确保每一列的 `1` 占多数。
1159+
11551160

11561161
|===
11571162

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.diguage.algo.leetcode;
2+
3+
public class _0861_ScoreAfterFlippingMatrix {
4+
// tag::answer[]
5+
/**
6+
* @author D瓜哥 · https://www.diguage.com
7+
* @since 2026-08-12 22:30:19
8+
*/
9+
public int matrixScore(int[][] grid) {
10+
int m = grid.length;
11+
int n = grid[0].length;
12+
// 确保第一行都是 1
13+
for (int[] row : grid) {
14+
if (row[0] == 1) {
15+
continue;
16+
}
17+
for (int i = 0; i < n; i++) {
18+
row[i] ^= 1;
19+
}
20+
}
21+
// 确保每一列的 1 占多数
22+
for (int i = 0; i < n; i++) {
23+
int cnt = 0;
24+
for (int[] ints : grid) {
25+
cnt += ints[i];
26+
}
27+
if (m / 2 < cnt) {
28+
continue;
29+
}
30+
for (int j = 0; j < m; j++) {
31+
grid[j][i] ^= 1;
32+
}
33+
}
34+
// 转成数字。⚠️:这一步可以和上一步合并,减少一次循环
35+
int result = 0;
36+
for (int[] row : grid) {
37+
int cnt = 0;
38+
for (int i = 0; i < n; i++) {
39+
cnt = cnt * 2 + row[i];
40+
}
41+
result += cnt;
42+
}
43+
return result;
44+
}
45+
// end::answer[]
46+
47+
static void main() {
48+
new _0861_ScoreAfterFlippingMatrix()
49+
// .matrixScore(new int[][]{{0, 0, 1, 1}, {1, 0, 1, 0}, {1, 1, 0, 0}});
50+
.matrixScore(new int[][]{{0, 1}, {1, 1}});
51+
}
52+
}

0 commit comments

Comments
 (0)