-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy path1254-number-of-closed-islands.js
57 lines (51 loc) · 1.49 KB
/
1254-number-of-closed-islands.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/**
* 1254. Number of Closed Islands
* https://leetcode.com/problems/number-of-closed-islands/
* Difficulty: Medium
*
* Given a 2D grid consists of 0s (land) and 1s (water). An island is a maximal 4-directionally
* connected group of 0s and a closed island is an island totally (all left, top, right, bottom)
* surrounded by 1s.
*
* Return the number of closed islands.
*/
/**
* @param {number[][]} grid
* @return {number}
*/
var closedIsland = function(grid) {
const rows = grid.length;
const cols = grid[0].length;
for (let i = 0; i < rows; i++) {
markBorderConnected(i, 0);
markBorderConnected(i, cols - 1);
}
for (let j = 0; j < cols; j++) {
markBorderConnected(0, j);
markBorderConnected(rows - 1, j);
}
let result = 0;
for (let i = 1; i < rows - 1; i++) {
for (let j = 1; j < cols - 1; j++) {
result += countClosed(i, j);
}
}
return result;
function markBorderConnected(row, col) {
if (row < 0 || row >= rows || col < 0 || col >= cols || grid[row][col] !== 0) return;
grid[row][col] = 1;
markBorderConnected(row - 1, col);
markBorderConnected(row + 1, col);
markBorderConnected(row, col - 1);
markBorderConnected(row, col + 1);
}
function countClosed(row, col) {
if (row < 0 || row >= rows || col < 0 || col >= cols || grid[row][col] !== 0) return 0;
grid[row][col] = 1;
countClosed(row - 1, col);
countClosed(row + 1, col);
countClosed(row, col - 1);
countClosed(row, col + 1);
return 1;
}
};