O(mn) with HashMap,java


  • 1
    public class Solution {
        public int findBlackPixel(char[][] picture, int N) {
            int m = picture.length,n = picture[0].length;
            if(m == 0 || n == 0) return 0;
            Map<Integer,Integer> row = new HashMap<>();
            Map<Integer,Integer> col = new HashMap<>();
            Map<String,Integer> map = new HashMap<>();
            StringBuilder sb = new StringBuilder();
            String[] strs = new String[m];
            int result = 0;
            for(int i = 0;i<=m-1;i++){
                sb.delete(0,sb.length());
                for(int j = 0;j<=n-1;j++){
                    sb.append(picture[i][j]);
                    if(picture[i][j] == 'B'){
                        row.put(i,row.getOrDefault(i,0)+1);
                        col.put(j,col.getOrDefault(j,0)+1);
                    }
                }
                strs[i] = sb.toString(); 
                map.put(strs[i],map.getOrDefault(strs[i],0)+1);
            }
            for(int i = 0;i<=m-1;i++){
                for(int j = 0;j<=n-1;j++){
                    if(picture[i][j] == 'B' && row.get(i) == N && row.get(i) == col.get(j) &&  map.get(strs[i]) == N){
                        result++;
                    }
                }
            }
            return result;
        }
    }
    

Log in to reply
 

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