C++_unordered_map_Time: O(mn), Space: O(m+n)


  • 0
    class Solution {
    public:
    int findBlackPixel(vector<vector<char>>& picture, int N) {
        if(picture.empty() || N <= 0) return 0;
        int m = picture.size();
        int n = picture[0].size();
        vector<int> rows(m,0);
        vector<int> cols(n,0);
        unordered_map<string, int> mp;//store the rows information
        for(int i = 0; i < m; ++i){
            string s = "";
            for(int j = 0; j < n; ++j){
                s += picture[i][j];
                if(picture[i][j] == 'B'){rows[i]++; cols[j]++;}
            }
            mp[s]++;
        }
        int res = 0;
        for(auto s : mp){
            if(s.second == N && std::count(s.first.begin(), s.first.end(), 'B') == N){
                string ss = s.first;
                for(int j = 0; j < n; ++j){
                    if(ss[j] == 'B'){
                        if(cols[j] == 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.