Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
1.1 kB
2
Indexable
Never
class Solution {
public:
    int matrixScore(vector<vector<int>>& grid) {
        int m=grid.size();//3
        int n=grid[0].size();//4
        for(int i=0;i<m;i++){//[0,1,2]
            if(grid[i][0]==0){
                for(int j=0;j<n;j++){
                    if(grid[i][j]==0)grid[i][j]=1;
                    else grid[i][j]=0;
                }
            }
        }
        int res1=0;
        for(int x=0;x<m;x++){
            for(int y=0;y<n;y++){
                res1=res1+grid[x][y]*pow(2,y);
            }
        }
        for(int i=0;i<n;i++){
            int s=0;
            for(int j=0;j<m;j++){
                s=s+grid[j][i];
            }
            if(s<(m/2)){
                for(int j=0;j<m;j++){
                    if(grid[j][i]==0)grid[j][i]=1;
                    else grid[j][i]=0;
                }
            }
        }
        int res=0;
        for(int x=0;x<m;x++){
            for(int y=0;y<n;y++){
                res=res+grid[x][y]*pow(2,y);
            }
        }
        return max(res1,res);
    }
};
Leave a Comment