Leetcode 531 Solution

This article provides solution to leetcode question 531 (lonely-pixel-i)

https://leetcode.com/problems/lonely-pixel-i

Solution

class Solution {
public:
    int findLonelyPixel(vector<vector<char>>& picture) {
        if (picture.size() == 0 || picture[0].size() == 0)
            return 0;

        int m = picture.size();
        int n = picture[0].size();
        vector<int> row(m);
        vector<int> col(n);

        for (int i = 0; i < m; i++)
            for (int j = 0; j < n; j++)
                if (picture[i][j] == 'B')
                    row[i]++, col[j]++;

        int res = 0;
        for (int i = 0; i < m; i++)
            for (int j = 0; j < n; j++)
                if (picture[i][j] == 'B' && row[i] == 1 && col[j] == 1)
                    res++;
        return res;
    }
};