Skip to content

Instantly share code, notes, and snippets.

@RP-3
Created July 7, 2020 07:44
Show Gist options
  • Save RP-3/6f1e24cc459d8e4ad9d93bd893e73283 to your computer and use it in GitHub Desktop.
Save RP-3/6f1e24cc459d8e4ad9d93bd893e73283 to your computer and use it in GitHub Desktop.
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