Java 26ms


  • 0
    C
    public int findLonelyPixel(char[][] picture) {
            
           
            if (picture == null || picture.length == 0 || picture[0].length == 0){
                return 0;
            }
            int n = picture.length;
            int m = picture[0].length;
            
            int[] col = new int[m];
            int[] row = new int[n];
            
            for (int i = 0; i < n; i++){
                
                for (int j = 0; j < m; j++){
                    
                    if (picture[i][j] == 'W') continue;
                    
                    if (col[j] == 0){
                        col[j] = 1;
                    }else{
                        col[j] = -1;
                    }
                    
                    if (row[i] == 0){
                        row[i] = 1;
                    }else{
                        row[i] = -1;
                    }
                    
                    
                }
                
            }
            
            int rowSum = 0;
            for (int i = 0; i < row.length; i++){
                rowSum += row[i] > 0? row[i]: 0;
            }
            
            int colSum = 0;
            for (int j = 0; j < col.length; j++){
                colSum += col[j] > 0? col[j]: 0;
            }
            
            return Math.min(rowSum, colSum);
        
        
        }
        ```

Log in to reply
 

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