C++, Time Complexity - O(mn) , Space Complexity - O(1)


  • 0
    J
    class Solution {
    public:
        int findLonelyPixel(vector<vector<char>>& picture) {
            if(picture.empty()) return 0;
            int rows = picture.size(), cols = picture[0].size();
            int count = 0;
            for(int i = 0; i < rows; i++){
                for(int j = 0; j < cols; j++){
                    if(picture[i][j] == 'B'){
                      int res = helper(picture, i, j); 
                      count += res;
                    }
                }
            }
            return count;
            
        }
    private:
        int helper(vector<vector<char>>& picture, int i, int j){
            int rows = picture.size(), cols = picture[0].size();
            for(int k = 0; k < rows; k++){
                if(k == i) continue;
                if(picture[k][j] == 'B') return 0;
            }
            for(int k = 0; k < cols; k++){
                if(k == j) continue;
                if(picture[i][k] == 'B') return 0;
            }
           return 1;
            
        }
    };
    

Log in to reply
 

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