Javascript solution using hash table


  • 0
    S
    /**
     * @param {character[][]} picture
     * @param {number} N
     * @return {number}
     */
    var findBlackPixel = function(picture, N) {
        if (!picture || picture.length === 0) { return 0; }
        
        let count = 0;
        let r = picture.length;
        let c = picture[0].length;
        
        let hash = {};
        for (let i = 0; i < r; i++) {
            let key = '';
            for (let j = 0; j < c; j++) {
                key += picture[i][j];
            }
            if (hash.hasOwnProperty(key)) {
                hash[key]++;
            } else {
                hash[key] = 1;
            }
        }
        
        for (let i = 0; i < Object.keys(hash).length; i++) {
            let key = Object.keys(hash)[i];
            if (hash[key] === N && countBinRow(key) === N) {
                count += countBinCol(key, picture, N);
            }
        }
        
        return count;
    };
    
    var countBinRow = function(row) {
        let count = 0;
        for (let i = 0; i < row.length; i++) {
            if (row[i] === 'B') { count++; }
        }
        
        return count;
    };
    
    var countBinCol = function(row, picture, N) {
        let count = 0;
        for (let i = 0; i < row.length; i++) {
            if (row[i] === 'B') {
                let Bcount = 0;
                for (let j = 0; j < picture.length; j++) {
                    if (picture[j][i] === 'B') { Bcount++; }
                }
                if (Bcount === N) { count += N; }
            }
        }
        
        return count;
    };
    

Log in to reply
 

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