Naive Java Solution O(MN)


  • 0
    public class Solution {
        public int findLonelyPixel(char[][] picture) {
            if (picture == null || picture.length == 0 || picture[0].length == 0) return 0;
            int m = picture.length, n = picture[0].length;
            Map<Integer, Integer> rows = new HashMap<>();
            Map<Integer, Integer> cols = new HashMap<>();
            int res = 0;
            for (int i = 0; i < m; i++) {
                for (int j = 0; j < n; j++) {
                    if (picture[i][j] == 'B') {
                        rows.put(i, rows.getOrDefault(i, 0) + 1);
                        cols.put(j, cols.getOrDefault(j, 0) + 1);
                    }
                }
            }
            for (int i = 0; i < m; i++) {
                for (int j = 0; j < n; j++) {
                    if (picture[i][j] == 'B') {
                        if (rows.get(i) == 1 && cols.get(j) == 1) res++;
                    }
                }
            }
            return res;
        }
    }
    

Log in to reply
 

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