We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 2d0e0cc commit 67e7d6bCopy full SHA for 67e7d6b
javascript/0463-island-perimeter.js
@@ -0,0 +1,23 @@
1
+var islandPerimeter = function(grid) {
2
+ const visit = new Set();
3
+
4
+ const dfs = function(i, j) {
5
+ if(i >= grid.length || j >= grid[0].length || i < 0 || j < 0 || grid[i][j] == 0)
6
+ return 1;
7
+ let flatCoord = i*grid[0].length + j;
8
+ if(visit.has(flatCoord))
9
+ return 0;
10
11
+ visit.add(flatCoord);
12
+ let perim = dfs(i, j + 1);
13
+ perim += dfs(i + 1, j);
14
+ perim += dfs(i, j - 1);
15
+ perim += dfs(i - 1, j);
16
+ return perim;
17
+ };
18
19
+ for(let i = 0; i < grid.length; i++)
20
+ for(let j = 0; j < grid[0].length; j++)
21
+ if(grid[i][j])
22
+ return dfs(i, j);
23
+};
0 commit comments