※ 引述《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