Iteration with space / T : O(NM), S : O(N + M)


  • 0
    J
    public class Solution {
        public int findLonelyPixel(char[][] picture) {
            int[] row = new int[picture.length];
            int[] col = new int[picture[0].length];
            
            for(int i = 0; i < picture.length; i++){
                for(int j = 0; j < picture[0].length; j++){
                    if(picture[i][j] == 'B'){
                        row[i]++;
                        col[j]++;
                    }
                }
            }
            
            int count = 0;
            for(int i = 0; i < picture.length; i++){
                for(int j = 0; j < picture[0].length; j++){
                    if(picture[i][j] == 'B' && row[i] == 1 && col[j] == 1){
                        count++;
                    }
                }
            }
            return count;
        }
    }
    

Log in to reply
 

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