Skip to content

Commit 834be45

Browse files
authored
Implement islandPerimeter function to calculate perimeter
1 parent b71c10d commit 834be45

1 file changed

Lines changed: 27 additions & 0 deletions

File tree

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* @param {number[][]} grid
3+
* @return {number}
4+
*/
5+
var islandPerimeter = function(grid) {
6+
let count = 0;
7+
8+
for (let i = 0; i<grid.length; i++){
9+
for (let j = 0; j<grid[0].length; j++){
10+
// land 일 때
11+
if (grid[i][j] == 1){
12+
count += 4;
13+
14+
if (j>0 && grid[i][j-1] == 1){
15+
count -= 1;
16+
} if (i>0 && grid[i-1][j] == 1){
17+
count -= 1;
18+
} if (j<grid[0].length-1 && grid[i][j+1] == 1){
19+
count -= 1;
20+
} if (i<grid.length-1 && grid[i+1][j] == 1){
21+
count -= 1;
22+
}
23+
}
24+
}
25+
}
26+
return count;
27+
};

0 commit comments

Comments
 (0)