C++ simple solution O(M*N)


  • 0
    G
    class Solution {
    public:
        int findLonelyPixel(vector<vector<char>>& picture) 
        {
            vector<int > Rows;
            
            for(int i = 0; i < picture.size(); ++i)
            {
                int Count = 0;
                int Col = 0;
                
                for(int j = 0; j < picture[i].size(); ++j)
                {
                    if(picture[i][j] == 'B')
                    {
                        Count++;
                        Col = j;
                    }
                }
                
                if(Count == 1)
                {
                    Rows.push_back(Col);
                }
            }
                
            int Total = 0;
            
            for(auto &Col : Rows)
            {
                int Count = 0;
            
                for(int i = 0; i < picture.size(); ++i)
                {
                    if(picture[i][Col] == 'B')
                    {
                        Count++;
                    }
                }
                
                if(Count == 1)
                {
                    Total++;
                }
            }
            
            return (Total);
        }
    };

Log in to reply
 

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