Javascript solution using O(3*X*Y)


  • 0
    S
    /**
     * @param {character[][]} picture
     * @return {number}
     */
    var findLonelyPixel = function(picture) {
        // edge case
        if (!picture || picture.length === 0) { return 0; }
        
        let xCount = [];
        let yCount = [];
        for (let x = 0; x < picture.length; x++) {
            for (let y = 0; y < picture[0].length; y++) {
                xCount.push(0);
                yCount.push(0);
            }
        }
        
        for (let x = 0; x < picture.length; x++) {
            for (let y = 0; y < picture[0].length; y++) {
                if (picture[x][y] === 'B') {
                    xCount[x]++;
                    yCount[y]++;
                }
            }
        }
        
        let count = 0;
        for (let x = 0; x < picture.length; x++) {
            for (let y = 0; y < picture[0].length; y++) {
                if (picture[x][y] === 'B' && xCount[x] === 1 && yCount[y] === 1) {
                    count++;
                }
            }
        }
        
        return count;
    };
    

Log in to reply
 

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