O(1) space C++ solution


  • 0
    D

    This question is similar to bomb the enemy.

    class Solution {
    public:
        int findLonelyPixel(vector<vector<char>>& picture) {
            if (picture.empty() || picture[0].empty())
                return 0;
    
            int m = picture.size();
            int n = picture[0].size();
            int ans = 0;
    
            for (int i = 0; i < m; ++i) {
                int col = -1; // which col has the single black pixel in row i
                for (int j = 0; j < n; ++j) {
                    if (picture[i][j] == 'B') {
                        if (col >= 0) {
                            col = -1;
                            break;
                        }
                        else col = j;
                    }
                }
                if (col >= 0) {
                    int count = 0;
                    for (int k = 0; k < m; ++k) {
                        if (picture[k][col] == 'B')
                            if (count++ == 1) break;
                    }
                    if (count == 1)
                        ans++;
                }
            }
            return ans;
        }
    };
    

Log in to reply
 

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