Easy solution in Java using arrays


  • 0
    O
    public class Solution {
        public int findBlackPixel(char[][] board, int N) {
            if(N == 0){
                return 0;
            }
            int count = 0;
            int m = board.length;
            if(m == 0){
                return 0;
            }
            int n = board[0].length;
            int[] rowH = new int[m];
            int[] colH = new int[n];
            for(int i = 0; i < m; i++){
                for(int j = 0; j < n; j++){
                    if(board[i][j] == 'B'){
                        rowH[i]++;
                        colH[j]++;
                    }
                }
            }
            for(int i = 0; i < m; i++){
                for(int j = 0; j < n; j++){
                    if(board[i][j] == 'B' && rowH[i] == N && colH[j] == N){
                        boolean isValid = true;
                        for(int k = 0; k < m; k++){
                            if(!isValid){
                                break;
                            }
                            if(k == i){
                                continue;
                            }
                            if(board[k][j] != 'B'){
                                continue;
                            }
                            for(int d = 0; d < n; d++){
                                if(board[k][d] != board[i][d]){
                                    isValid = false;
                                    break;
                                }
                            }
                        }
                        if(isValid){
                            count++;
                        }
                    }
                }
            }
            return count;
        }
    }
    

Log in to reply
 

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