861:翻转矩阵后的得分

This commit is contained in:
huangge1199@hotmail.com 2021-09-08 22:13:13 +08:00
parent 7792a21b38
commit 0179848761
2 changed files with 96 additions and 0 deletions

View File

@ -0,0 +1,65 @@
//有一个二维矩阵 A 其中每个元素的值为 0 1
//
// 移动是指选择任一行或列并转换该行或列中的每一个值将所有 0 都更改为 1将所有 1 都更改为 0
//
// 在做出任意次数的移动后将该矩阵的每一行都按照二进制数来解释矩阵的得分就是这些数字的总和
//
// 返回尽可能高的分数
//
//
//
//
//
//
// 示例
//
// 输入[[0,0,1,1],[1,0,1,0],[1,1,0,0]]
//输出39
//解释
//转换为 [[1,1,1,1],[1,0,0,1],[1,1,1,1]]
//0b1111 + 0b1001 + 0b1111 = 15 + 9 + 15 = 39
//
//
//
// 提示
//
//
// 1 <= A.length <= 20
// 1 <= A[0].length <= 20
// A[i][j] 0 1
//
// Related Topics 贪心 位运算 数组 矩阵 👍 206 👎 0
package leetcode.editor.cn;
//861:翻转矩阵后的得分
class ScoreAfterFlippingMatrix {
public static void main(String[] args) {
//测试代码
Solution solution = new ScoreAfterFlippingMatrix().new Solution();
}
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public int matrixScore(int[][] grid) {
int m = grid.length, n = grid[0].length;
int ret = m * (1 << (n - 1));
for (int j = 1; j < n; j++) {
int nOnes = 0;
for (int[] ints : grid) {
if (ints[0] == 1) {
nOnes += ints[j];
} else {
nOnes += (1 - ints[j]);
}
}
int k = Math.max(nOnes, m - nOnes);
ret += k * (1 << (n - j - 1));
}
return ret;
}
}
//leetcode submit region end(Prohibit modification and deletion)
}

View File

@ -0,0 +1,31 @@
<p>有一个二维矩阵&nbsp;<code>A</code> 其中每个元素的值为&nbsp;<code>0</code>&nbsp;&nbsp;<code>1</code>&nbsp;</p>
<p>移动是指选择任一行或列,并转换该行或列中的每一个值:将所有 <code>0</code> 都更改为 <code>1</code>,将所有 <code>1</code> 都更改为 <code>0</code></p>
<p>在做出任意次数的移动后,将该矩阵的每一行都按照二进制数来解释,矩阵的得分就是这些数字的总和。</p>
<p>返回尽可能高的分数。</p>
<p>&nbsp;</p>
<ol>
</ol>
<p><strong>示例:</strong></p>
<pre><strong>输入:</strong>[[0,0,1,1],[1,0,1,0],[1,1,0,0]]
<strong>输出:</strong>39
<strong>解释:
</strong>转换为 [[1,1,1,1],[1,0,0,1],[1,1,1,1]]
0b1111 + 0b1001 + 0b1111 = 15 + 9 + 15 = 39</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ol>
<li><code>1 &lt;= A.length &lt;= 20</code></li>
<li><code>1 &lt;= A[0].length &lt;= 20</code></li>
<li><code>A[i][j]</code>&nbsp;&nbsp;<code>0</code>&nbsp;<code>1</code></li>
</ol>
<div><div>Related Topics</div><div><li>贪心</li><li>位运算</li><li>数组</li><li>矩阵</li></div></div><br><div><li>👍 206</li><li>👎 0</li></div>