贪心
/*
* @lc app=leetcode id=861 lang=cpp
*
* [861] Score After Flipping Matrix
*/
// @lc code=start
class Solution {
public:
int calculate(vector<vector<int>>& grid) {
int N = grid.size();
int M = grid[0].size();
int ans = 0;
for(int i=0;i<N;i++){
int row = 0;
for(int j=0;j<M;j++){
row <<= 1;
row += grid[i][j];
}
ans += row;
}
return ans;
}
bool transRow(vector<vector<int>>& grid) {
int N = grid.size();
int M = grid[0].size();
bool ans = false;
for(int i=0;i<N;i++){
int a = 0,b = 0;
for(int j=0;j<M;j++){
a += grid[i][j];
b += (grid[i][j] == 0 ? 1 : 0 );
a <<= 1;
b <<= 1;
}
if(b > a){
for(int j=0;j<M;j++){
grid[i][j] = (grid[i][j] == 0 ? 1 : 0 );
}
ans = true;
}
}
return ans;
}
bool transColumn(vector<vector<int>>& grid) {
int N = grid.size();
int M = grid[0].size();
bool ans = false;
for(int j=0;j<M;j++){
int cnt = 0;
for(int i=0;i<N;i++){
cnt += (grid[i][j] == 0 ? 1 : 0 );
}
if(cnt > N/2){
for(int i=0;i<N;i++){
grid[i][j] = (grid[i][j] == 0 ? 1 : 0 );
}
ans = true;
}
}
return ans;
}
int matrixScore(vector<vector<int>>& grid) {
bool flagRow, flagColumn;
do {
flagRow = transRow(grid);
//print(grid);
flagColumn = transColumn(grid);
//print(grid);
}while(flagRow || flagColumn) ;
return calculate(grid);
}
};
// @lc code=end