Created
July 7, 2020 07:44
-
-
Save RP-3/6f1e24cc459d8e4ad9d93bd893e73283 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var islandPerimeter = function(grid) { | |
const countWateryNeighbors = (row, col) => { | |
return [[row+1, col],[row-1, col],[row, col+1],[row, col-1]] | |
.map(([r, c]) => { | |
if(r < 0 || r >= grid.length || c < 0 || c >= grid[0].length) return 1; | |
if(grid[r][c] === 0) return 1; | |
return 0; | |
}) | |
.reduce((a, b) => a + b); | |
}; | |
let result = 0; | |
for(let row = 0; row < grid.length; row++){ | |
for(let col = 0; col < grid[0].length; col++){ | |
result += grid[row][col] ? countWateryNeighbors(row, col) : 0; | |
} | |
} | |
return result; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment