※ 引述《JIWP (神楽めあ的钱包)》之铭言:
: 861. Score After Flipping Matrix
: 有一个m*n的matrix
: 每一列都代表一个二进制数
: 可以把任意行、列的元素反转(0->1、1->0)
: 请问在进行反转后,这个matrix总和最大为多少?
好久没写了 看一下其他人的思路才写出来
C++ code:
class Solution {
public:
int matrixScore(vector<vector<int>>& grid) {
int cn = grid.size();
int rn = grid[0].size();
for (int i = 0; i < cn; i++) {
if (grid[i][0] == 0) {
for (int j = 0; j < rn; j++) {
grid[i][j] = (grid[i][j] + 1) % 2;
}
}
}
for (int i = 1; i < rn; i++) {
int count = 0;
for (int j = 0; j < cn; j++) {
if (grid[j][i] == 0) {
count ++;
}
}
if (count > (cn / 2)) {
for (int j = 0; j < cn; j++) {
grid[j][i] = (grid[j][i] + 1) % 2;
}
}
}
int res = 0;
for (int i = 0; i < cn; i++){
int index = rn - 1;
for (int j = 0; j < rn; j++) {
res += grid[i][j] * pow(2, index);
index