861. Score After Flipping Matrix
Posted ruruozhenhao
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了861. Score After Flipping Matrix相关的知识,希望对你有一定的参考价值。
We have a two dimensional matrix A
where each value is 0
or 1
.
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.
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.
Return the highest possible score.
Example 1:
Input: [[0,0,1,1],[1,0,1,0],[1,1,0,0]]
Output: 39
Explanation:
Toggled to [[1,1,1,1],[1,0,0,1],[1,1,1,1]].
0b1111 + 0b1001 + 0b1111 = 15 + 9 + 15 = 39
Note:
1 <= A.length <= 20
1 <= A[0].length <= 20
A[i][j]
is0
or1
.
Approach #1: C++.
class Solution { public: int matrixScore(vector<vector<int>>& A) { int r = A.size(), c = A[0].size(); int ans = 0; for (int i = 0; i < c; ++i) { int col = 0; for (int j = 0; j < r; ++j) col += A[j][i] ^ A[j][0]; //相同为0, 不同为1。 ans += max(col, r - col) * (1 << (c - 1 - i)); } return ans; } };
以上是关于861. Score After Flipping Matrix的主要内容,如果未能解决你的问题,请参考以下文章
861. Score After Flipping Matrix
算法leetcode861. 翻转矩阵后的得分(多语言实现)
算法leetcode861. 翻转矩阵后的得分(多语言实现)
[LeetCode] Score After Flipping Matrix 翻转数组后的分数