Java one pass solution, O(mn),O(m+n)


  • 0

    just compare number of rows and columns that has only 1 'B'

    class Solution {
        public int findLonelyPixel(char[][] picture) {
            if(picture == null || picture.length == 0){return 0;}
            int row = picture.length;
            int col = picture[0].length;
            int[] check_row = new int[row];
            int count_row = 0;
            int[] check_col = new int[col];
            int count_col = 0;
            for(int i = 0; i < row; i++){
                for(int j = 0; j < col; j++){
                    if(picture[i][j] == 'B'){
                        check_row[i] += 1;
                        if(check_row[i] == 1){count_row++;}
                        if(check_row[i] == 2){count_row--;}
                        check_col[j] += 1;
                        if(check_col[j] == 1){count_col++;}
                        if(check_col[j] == 2){count_col--;}
                    }
                }
            }
    
            return Math.min(count_row,count_col);
        }
    }
    
    

  • 0
    T

    try ["WBB","BWW","BWW"]
    it will give wrong answer


Log in to reply
 

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