Straight-forward C++ O(mn) solution


  • 0
    M
        int findBlackPixel(vector<vector<char>>& picture, int N) {
            int res=0;
            map<vector<int>, vector<int>> dict;
            vector<int> col(picture[0].size(), 0);
            vector<int> temp;
            for(int i=0;i<picture.size();i++) {
                for(int j=0;j<picture[0].size();j++) {
                    if(picture[i][j]=='B') {
                        col[j]++;
                        temp.push_back(j);
                    }
                }
                if(dict.find(temp)==dict.end()) {
                    vector<int> t(1, i);
                    dict[temp]=t;
                }
                else dict[temp].push_back(i);
                temp.clear();
            }
            for(map<vector<int>, vector<int>>::iterator p=dict.begin();p!=dict.end();p++) {
                vector<int> a=p->first, b=p->second; 
                if(a.size()!=N||a.size()!=b.size()) continue;
                for(int i=0;i<a.size();i++) {
                    if(col[a[i]]==N) res+=N;
                }
            }
            return res;
        }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.