Solution in C++ (Simple logic)


#1

int Solution::solve(vector<vector > &A) {

int ans = 0;
int r = A.size();
int c = A[0].size();

int row_independent[r][c];

for( int j=0; j<c; j++ ){
    
    int cnt = 0;
    
    for( int i=0; i<r; i++ ){
        
        if( A[i][j] == 1 ){
            
            cnt = A[i][j] + cnt;
        }
        
        else if( A[i][j] == 0 ){
            
            cnt = 0;
        }
        
        row_independent[i][j] = cnt;
    }
}

for( int i=0; i<r; i++ ){
    
    sort( row_independent[i]+0, row_independent[i]+c, greater<int>() );
}

int currarea=0;

for( int i=0; i<r; i++ ){
    
    for( int j=0; j<c; j++ ){
        
        currarea = ( j+1 ) * row_independent[i][j];
        
        if( currarea > ans )
            ans = currarea;
    }
}

return ans;

}

Based on logic shared in the “Solution Approach” tab in the question.