Concise C++ O(m*n) solution


  • 0
    H
    class Solution {
    public:
        int findLonelyPixel(vector<vector<char>>& picture) {
            int res = 0;
            int m = picture.size();
            if (m == 0) return res;
            int n = picture[0].size();
            vector<int> row(m, 0), col(n, 0);
            for (int i = 0; i < m; i++) {
                for (int j = 0; j < n; j++) {
                    if (picture[i][j] == 'B') {
                        row[i]++;
                        col[j]++;
                    }
                }
            }
            for (int i = 0; i < m; i++) {
                if (row[i] != 1) continue; // early termination to save time
                for (int j = 0; j < n; j++) {
                    if (picture[i][j] == 'B' && col[j] == 1) res++;
                }
            }
            return res;
        }
    };
    

Log in to reply
 

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