Skip to content

Commit 1393236

Browse files
committed
Update index.ts
1 parent 1f6fd61 commit 1393236

File tree

1 file changed

+30
-1
lines changed

1 file changed

+30
-1
lines changed

number-of-closed-islands/index.ts

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,31 @@
1-
function closedIsland(grid: number[][]): number {}
1+
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+
}
231
export default closedIsland;

0 commit comments

Comments
 (0)