O(mn) Javascript solution


  • 0
    D
    /**
     * @param {number[][]} grid
     * @return {number}
     */
    var islandPerimeter = function(grid) {
        var edges = 0;
        grid.forEach((row, r) => {
            row.forEach((col, c) => {
                var left = grid[r][c-1] || 0;
                var right = grid[r][c+1] || 0;
                var top = (grid[r-1] || [])[c] || 0;
                var bottom = (grid[r-1] || [])[c] || 0;
                if (col === 1) {
                    edges += (4 - top - bottom - left - right)
                }
            })
        });
        return edges;
    };
    

Log in to reply
 

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