We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 2d0e0cc commit fa862c7Copy full SHA for fa862c7
javascript/1905-count-sub-islands.js
@@ -0,0 +1,35 @@
1
+var countSubIslands = function(grid1, grid2) {
2
+ let ROWS = grid1.length, COLS = grid1[0].length;
3
+ let visit = new Set();
4
+
5
+ const dfs = function(r, c) {
6
+ let flatCoord = r*COLS + c;
7
+ if (
8
+ r < 0
9
+ || c < 0
10
+ || r == ROWS
11
+ || c == COLS
12
+ || grid2[r][c] == 0
13
+ || visit.has(flatCoord)
14
+ )
15
+ return true;
16
17
+ visit.add(flatCoord);
18
+ let res = true;
19
+ if(grid1[r][c] == 0)
20
+ res = false;
21
22
+ res = dfs(r - 1, c) && res;
23
+ res = dfs(r + 1, c) && res;
24
+ res = dfs(r, c - 1) && res;
25
+ res = dfs(r, c + 1) && res;
26
+ return res;
27
+ };
28
29
+ let count = 0;
30
+ for(let r = 0; r < ROWS; r++)
31
+ for(let c = 0; c < COLS; c++)
32
+ if(grid2[r][c] && !visit.has(r*COLS + c) && dfs(r, c))
33
+ count += 1;
34
+ return count;
35
+};
0 commit comments