O(MN) time & O(N) space solution with hash map


  • 0

    unordered_map<int, bool> colIndex records whether column index i has a lonely black pixel.

        int findLonelyPixel(vector<vector<char>>& p) {
          unordered_map<int, bool> colIndex;
          int count = 0;
          for (auto row : p) {
            bool single = std::count(row.begin(), row.end(), 'B') == 1;
            for (int i = 0; i < row.size(); ++i) {
              if (row[i]=='B') {
                if (colIndex.count(i)) {
                    count -= colIndex[i];
                    colIndex[i] = false;
                }
                else count += (colIndex[i] = single);
              }
            }
          }
          return count;
        }
    

Log in to reply
 

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