diff --git a/solution/1900-1999/1905.Count Sub Islands/README.md b/solution/1900-1999/1905.Count Sub Islands/README.md index 90a0319ccf599..4e53fbb3a2b3d 100644 --- a/solution/1900-1999/1905.Count Sub Islands/README.md +++ b/solution/1900-1999/1905.Count Sub Islands/README.md @@ -41,7 +41,7 @@ grid2 中标红的 1 区域是子岛屿,总共有 3 个子岛屿。
示例 2:
输入:grid1 = [[1,0,1,0,1],[1,1,1,1,1],[0,0,0,0,0],[1,1,1,1,1],[1,0,1,0,1]], grid2 = [[0,0,0,0,0],[1,1,1,1,1],[0,1,0,1,0],[0,1,0,1,0],[1,0,0,0,1]] -输出:2 +输出:2 解释:如上图所示,左边为 grid1 ,右边为 grid2 。 grid2 中标红的 1 区域是子岛屿,总共有 2 个子岛屿。@@ -220,6 +220,35 @@ function countSubIslands(grid1: number[][], grid2: number[][]): number { } ``` +#### JavaScript + +```js +function countSubIslands(grid1, grid2) { + const [m, n] = [grid1.length, grid1[0].length]; + let ans = 0; + const dirs = [-1, 0, 1, 0, -1]; + const dfs = (i, j) => { + let ok = grid1[i][j]; + grid2[i][j] = 0; + for (let k = 0; k < 4; ++k) { + const [x, y] = [i + dirs[k], j + dirs[k + 1]]; + if (x >= 0 && x < m && y >= 0 && y < n && grid2[x][y]) { + ok &= dfs(x, y); + } + } + return ok; + }; + for (let i = 0; i < m; ++i) { + for (let j = 0; j < n; j++) { + if (grid2[i][j]) { + ans += dfs(i, j); + } + } + } + return ans; +} +``` + diff --git a/solution/1900-1999/1905.Count Sub Islands/README_EN.md b/solution/1900-1999/1905.Count Sub Islands/README_EN.md index f33ae14457631..23dcd7b838ac5 100644 --- a/solution/1900-1999/1905.Count Sub Islands/README_EN.md +++ b/solution/1900-1999/1905.Count Sub Islands/README_EN.md @@ -42,7 +42,7 @@ The 1s colored red in grid2 are those considered to be part of a sub-island. The
Input: grid1 = [[1,0,1,0,1],[1,1,1,1,1],[0,0,0,0,0],[1,1,1,1,1],[1,0,1,0,1]], grid2 = [[0,0,0,0,0],[1,1,1,1,1],[0,1,0,1,0],[0,1,0,1,0],[1,0,0,0,1]] -Output: 2 +Output: 2 Explanation: In the picture above, the grid on the left is grid1 and the grid on the right is grid2. The 1s colored red in grid2 are those considered to be part of a sub-island. There are two sub-islands.@@ -220,6 +220,35 @@ function countSubIslands(grid1: number[][], grid2: number[][]): number { } ``` +#### JavaScript + +```js +function countSubIslands(grid1, grid2) { + const [m, n] = [grid1.length, grid1[0].length]; + let ans = 0; + const dirs = [-1, 0, 1, 0, -1]; + const dfs = (i, j) => { + let ok = grid1[i][j]; + grid2[i][j] = 0; + for (let k = 0; k < 4; ++k) { + const [x, y] = [i + dirs[k], j + dirs[k + 1]]; + if (x >= 0 && x < m && y >= 0 && y < n && grid2[x][y]) { + ok &= dfs(x, y); + } + } + return ok; + }; + for (let i = 0; i < m; ++i) { + for (let j = 0; j < n; j++) { + if (grid2[i][j]) { + ans += dfs(i, j); + } + } + } + return ans; +} +``` + diff --git a/solution/1900-1999/1905.Count Sub Islands/Solution.js b/solution/1900-1999/1905.Count Sub Islands/Solution.js new file mode 100644 index 0000000000000..ffef4c11a8532 --- /dev/null +++ b/solution/1900-1999/1905.Count Sub Islands/Solution.js @@ -0,0 +1,24 @@ +function countSubIslands(grid1, grid2) { + const [m, n] = [grid1.length, grid1[0].length]; + let ans = 0; + const dirs = [-1, 0, 1, 0, -1]; + const dfs = (i, j) => { + let ok = grid1[i][j]; + grid2[i][j] = 0; + for (let k = 0; k < 4; ++k) { + const [x, y] = [i + dirs[k], j + dirs[k + 1]]; + if (x >= 0 && x < m && y >= 0 && y < n && grid2[x][y]) { + ok &= dfs(x, y); + } + } + return ok; + }; + for (let i = 0; i < m; ++i) { + for (let j = 0; j < n; j++) { + if (grid2[i][j]) { + ans += dfs(i, j); + } + } + } + return ans; +}