C++ 2 passes o(m*n) time o(1) space


  • 0
    N
    int findLonelyPixel(vector<vector<char>>& picture) {
            int res = INT_MAX;
            
            int m = picture.size();
            if (m == 0) return 0;
            
            int n = picture[0].size();
            if (n == 0) return 0;
            
            int sum = 0;
            for (int i = 0; i < m; i++)
            {
                int sumRow = 0;
                for (int j = 0; j < n; j++)
                {
                    sumRow += (picture[i][j] == 'B' ? 1 : 0);
                }
                if (sumRow == 1) sum++;
            }
            
            res = min(res, sum);
            
            sum = 0;
            for (int j = 0; j < n; j++)
            {
                int sumCol = 0;
                for (int i = 0; i < m; i++)
                {
                    sumCol += (picture[i][j] == 'B' ? 1 : 0);
                }
                if (sumCol == 1) sum++;
            }
            
            res = min(res, sum);
            return res;
        }
    

Log in to reply
 

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