Java O(m*n), O(m + n) space


  • 0
    V
    public class Solution {
        public int findLonelyPixel(char[][] picture) {
            int m = picture.length, count = 0;
            if (m > 0) {
                int n = picture[0].length;
    
                // Stores colum indices in case the row containes only one Black pixel
                int[] rowCheck = new int[m];
                
                // stores the number of Black pixels in he column j.
                int[] colCheck = new int[n];
                
                for (int i = 0; i < m; i++) {
                    for (int j = 0; j < n; j++) {
                        if (picture[i][j] == 'B') {
                            colCheck[j]++;
                            rowCheck[i] = ((rowCheck[i] == 0) ? j + 1 : -1); 
                        }
                    }
                }
                
                for(int i = 0; i < m; i++) {
                    if (rowCheck[i] > 0 && colCheck[rowCheck[i] - 1] == 1) count++; 
                }
            }
            
            return count;
        }
    }
    

Log in to reply
 

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