Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit b9b641c

Browse files
committedApr 7, 2025
Add solution #1254
1 parent 6d79fdd commit b9b641c

File tree

2 files changed

+59
-1
lines changed

2 files changed

+59
-1
lines changed
 

‎README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,198 LeetCode solutions in JavaScript
1+
# 1,199 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -956,6 +956,7 @@
956956
1250|[Check If It Is a Good Array](./solutions/1250-check-if-it-is-a-good-array.js)|Hard|
957957
1252|[Cells with Odd Values in a Matrix](./solutions/1252-cells-with-odd-values-in-a-matrix.js)|Easy|
958958
1253|[Reconstruct a 2-Row Binary Matrix](./solutions/1253-reconstruct-a-2-row-binary-matrix.js)|Medium|
959+
1254|[Number of Closed Islands](./solutions/1254-number-of-closed-islands.js)|Medium|
959960
1261|[Find Elements in a Contaminated Binary Tree](./solutions/1261-find-elements-in-a-contaminated-binary-tree.js)|Medium|
960961
1267|[Count Servers that Communicate](./solutions/1267-count-servers-that-communicate.js)|Medium|
961962
1268|[Search Suggestions System](./solutions/1268-search-suggestions-system.js)|Medium|
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/**
2+
* 1254. Number of Closed Islands
3+
* https://leetcode.com/problems/number-of-closed-islands/
4+
* Difficulty: Medium
5+
*
6+
* Given a 2D grid consists of 0s (land) and 1s (water). An island is a maximal 4-directionally
7+
* connected group of 0s and a closed island is an island totally (all left, top, right, bottom)
8+
* surrounded by 1s.
9+
*
10+
* Return the number of closed islands.
11+
*/
12+
13+
/**
14+
* @param {number[][]} grid
15+
* @return {number}
16+
*/
17+
var closedIsland = function(grid) {
18+
const rows = grid.length;
19+
const cols = grid[0].length;
20+
21+
for (let i = 0; i < rows; i++) {
22+
markBorderConnected(i, 0);
23+
markBorderConnected(i, cols - 1);
24+
}
25+
for (let j = 0; j < cols; j++) {
26+
markBorderConnected(0, j);
27+
markBorderConnected(rows - 1, j);
28+
}
29+
30+
let result = 0;
31+
for (let i = 1; i < rows - 1; i++) {
32+
for (let j = 1; j < cols - 1; j++) {
33+
result += countClosed(i, j);
34+
}
35+
}
36+
37+
return result;
38+
39+
function markBorderConnected(row, col) {
40+
if (row < 0 || row >= rows || col < 0 || col >= cols || grid[row][col] !== 0) return;
41+
grid[row][col] = 1;
42+
markBorderConnected(row - 1, col);
43+
markBorderConnected(row + 1, col);
44+
markBorderConnected(row, col - 1);
45+
markBorderConnected(row, col + 1);
46+
}
47+
48+
function countClosed(row, col) {
49+
if (row < 0 || row >= rows || col < 0 || col >= cols || grid[row][col] !== 0) return 0;
50+
grid[row][col] = 1;
51+
countClosed(row - 1, col);
52+
countClosed(row + 1, col);
53+
countClosed(row, col - 1);
54+
countClosed(row, col + 1);
55+
return 1;
56+
}
57+
};

0 commit comments

Comments
 (0)
Please sign in to comment.