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


  • 1
    class Solution {
    public:
    int findLonelyPixel(vector<vector<char>>& picture) {
        if(picture.empty()) return 0;
        int res = 0;
        int m = picture.size();
        int n = picture[0].size();
        vector<int> rows(m,0);
        vector<int> cols(n,0);
        for(int i = 0; i < m; ++i){
            for(int j = 0; j < n; ++j){
                if(cols[j] >= 2) continue;
                if(picture[i][j] == 'B'){rows[i]++; cols[j]++;}
            }
        }
        
        for(int i = 0; i < m; ++i){
            if(rows[i] != 1) continue;
            for(int j = 0; j < n; ++j){
                if(picture[i][j] == 'B'){
                    if(cols[j] == 1){
                        res++;
                    }
                    break;
                }
            }
        }
        return res;
    }
    };

Log in to reply
 

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