We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 1f6fd61 commit 1393236Copy full SHA for 1393236
number-of-closed-islands/index.ts
@@ -1,2 +1,31 @@
1
-function closedIsland(grid: number[][]): number {}
+function closedIsland(grid: number[][]): number {
2
+ const m = grid.length;
3
+ const n = grid[0].length;
4
+ let res = 0;
5
+ grid.forEach((a, i) =>
6
+ a.forEach((v, j) => {
7
+ if (v === 0) {
8
+ res += Number(dfs(i, j));
9
+ }
10
+ })
11
+ );
12
+ function dfs(i: number, j: number): boolean {
13
+ if (i < 0 || j < 0 || i >= m || j >= n) {
14
+ return false;
15
16
+ if (grid[i][j]) return true;
17
+ grid[i][j] = 1;
18
+
19
+ return [
20
+ [i - 1, j],
21
+ [i, j + 1],
22
+ [i + 1, j],
23
+ [i, j - 1],
24
+ ]
25
+ .map((v) => dfs(v[0], v[1]))
26
+ .reduce((a, v) => a && v);
27
28
29
+ return res;
30
+}
31
export default closedIsland;
0 commit comments