A simple Java Solution


  • 0
    A
    public class Solution {
        public int findBlackPixel(char[][] picture, int N) {
            int ans = 0;
            int r = picture.length;
            int c = picture[0].length;
            
            int[][] data = new int[r][c];
            
          int[] row = new int[r];
           int[] col = new int[c];
    HashMap<ArrayList<Integer>,Integer> map = new HashMap<ArrayList<Integer>,Integer>();
            for(int i=0;i<r;i++){
                int sum = 0;
                ArrayList<Integer> list = new ArrayList<Integer>();
                for(int j=0;j<c;j++){
                    if(picture[i][j] == 'W'){
                        data[i][j] = 0;
                        sum += 0;
                    }else{
                        sum += 1;
                        data[i][j] = 1;
                    }
                    list.add(data[i][j]);
                    
                }
                row[i] = sum;
                if(sum == N)
                if(map.containsKey(list)){
                    map.put(list,map.get(list)+1);
                }else{
                    map.put(list,1);
                }
            }
            
            for(int i=0;i<c;i++){
                int sum = 0;
                for(int j=0;j<r;j++){
                    if(data[j][i] == 0){
                        sum += 0;
                    }else{
                        sum += 1;
                    }
                }
                
                col[i] = sum;
            }
            
            for(ArrayList<Integer> key : map.keySet()){
                if((int)map.get(key) != N){
                    continue;
                }
                
                int k = 0;
                for(int i=0;i<c;i++){
                    if(key.get(i) == 1 && col[i] == N){
                        k++;
                    }
                }
                
                ans += k * N;
            }
            
            return ans;
        }
    }
    
    

Log in to reply
 

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