Straightforward C++ solution O(nm) Time


  • 0
    J
    int findLonelyPixel(vector<vector<char>>& picture) {
            int m = picture.size(), n = m? picture[0].size() : 0, cnt = 0;
            if(!m || !n) return cnt;
            vector<int> cols(n,0);  vector<int> rows(m,0);
            for(int i = 0; i < m; ++i)
                for(int j = 0; j < n; ++j)
                    if(picture[i][j] == 'B'){
                        cols[j]++;
                        rows[i]++;
                    }
            
            for(int i = 0; i < m; ++i){
                if(rows[i] != 1) continue;
                for(int j = 0; j < n; ++j){
                    if(cols[j] != 1) continue;
                    if(picture[i][j] == 'B') cnt++;
                }
            }
            return cnt;
        }
    

Log in to reply
 

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