LC 1252. Cells with Odd Values in a Matrix

Nilanjan Deb · April 28, 2020

This is my CPP solution.

class Solution {
public:
    int oddCells(int m, int n, vector<vector<int>>& indices) {
        int sum = 0, k = indices.size(); int oR= 0, oC = 0, eR=0, eC = 0;
        vector<int> row(m+1,0);
        vector<int> col(n+1,0);
        for(int i=0;i<k;i++){
            row[indices[i][0]]++;
            col[indices[i][1]]++;
        }
        for(int i=0;i<m;i++){
            if(row[i]%2 == 1)
                oR += 1;
            else
                eR += 1;
        }
        for(int i=0;i<n;i++){
            if(col[i]%2 == 1)
                oC += 1;
            else
                eC += 1;
        }
        sum += (oR * n);
        cout << sum << endl;
        if(oC){
            sum -= (oR*oC);
            cout << sum << endl;
            sum += (oC*eR);
        }
        return sum;
    }
};


Dicussion Forum