One pass O(mn) time O(n) space, 28ms


  • 0
    public class Solution {
        public int findLonelyPixel(char[][] picture) {
            if (picture.length == 0 || picture[0].length == 0) {
                return 0;
            }
            int[] mark = new int[picture[0].length];
            int result = 0;
            for (int i = 0; i < picture.length; i++) {
                int num = 0, index = -1;
                boolean singleCol = false;
                for (int j = 0; j < picture[0].length; j++) {
                    if (picture[i][j] == 'B') {
                        if (mark[j] == 0) {
                            mark[j] = 1;
                            singleCol = true;
                            index = j;
                        } else if (mark[j] == 2) {
                            result--;
                            mark[j] = 1;
                        }
                        num++;
                    }
                }
                if (num == 1 && singleCol) {
                    result++;
                    mark[index] = 2;
                }
            }
            return result;
        }
    }
    

Log in to reply
 

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