This approach uses a depth-first search (DFS) to mark land cells connected to the boundaries. We iterate over the boundary cells of the grid and if a land cell is found, we perform DFS to mark all connected land cells as visited. Finally, we count all remaining unvisited land cells inside the grid, which are considered enclaves.
Time Complexity: O(m * n) as each cell is visited at most once.
Space Complexity: O(m * n) in the worst case due to recursion stack used by DFS.
1public class Solution {
2 public int NumEnclaves(int[][] grid) {
3 int m = grid.Length;
4 int n = grid[0].Length;
5
6 void Dfs(int i, int j) {
7 if (i < 0 || j < 0 || i >= m || j >= n || grid[i][j] == 0) {
8 return;
9 }
10 grid[i][j] = 0;
11 Dfs(i + 1, j);
12 Dfs(i - 1, j);
13 Dfs(i, j + 1);
14 Dfs(i, j - 1);
15 }
16
17 for (int i = 0; i < m; i++) {
18 if (grid[i][0] == 1) {
19 Dfs(i, 0);
20 }
21 if (grid[i][n - 1] == 1) {
22 Dfs(i, n - 1);
23 }
24 }
25 for (int j = 0; j < n; j++) {
26 if (grid[0][j] == 1) {
27 Dfs(0, j);
28 }
29 if (grid[m - 1][j] == 1) {
30 Dfs(m - 1, j);
31 }
32 }
33
34 int count = 0;
35 for (int i = 0; i < m; i++) {
36 for (int j = 0; j < n; j++) {
37 if (grid[i][j] == 1) {
38 count++;
39 }
40 }
41 }
42 return count;
43 }
44}
45
This C# code implements similar logic: iterating over boundary cells and using DFS to mark reachable land cells. Enclaves are counted by checking the remaining unmarked land cells.
In this approach, we use a queue to perform BFS to mark land cells connected to the boundaries. By enqueueing each boundary land cell and exploring its neighbors iteratively, we can mark all reachable boundary-connected lands. This is followed by counting the unvisited land cells – those are the enclaves.
Time Complexity: O(m * n).
Space Complexity: O(min(m, n)) considering the queue size in the worst case.
1var numEnclaves = function(grid) {
2 const bfs = (startRow, startCol) => {
3 const queue = [];
4 queue.push([startRow, startCol]);
5 grid[startRow][startCol] = 0;
6 const directions = [[1, 0], [-1, 0], [0, 1], [0, -1]];
7
8 while (queue.length !== 0) {
9 const [r, c] = queue.shift();
10 for (const [dx, dy] of directions) {
11 const newRow = r + dx;
12 const newCol = c + dy;
13 if (newRow >= 0 && newRow < grid.length && newCol >= 0 && newCol < grid[0].length && grid[newRow][newCol] === 1) {
14 grid[newRow][newCol] = 0;
15 queue.push([newRow, newCol]);
16 }
17 }
18 }
19 };
20
21 const m = grid.length, n = grid[0].length;
22
23 for (let i = 0; i < m; i++) {
24 if (grid[i][0] === 1) bfs(i, 0);
25 if (grid[i][n - 1] === 1) bfs(i, n - 1);
26 }
27 for (let j = 0; j < n; j++) {
28 if (grid[0][j] === 1) bfs(0, j);
29 if (grid[m - 1][j] === 1) bfs(m - 1, j);
30 }
31
32 let count = 0;
33 for (let i = 0; i < m; i++) {
34 for (let j = 0; j < n; j++) {
35 if (grid[i][j] === 1) {
36 count++;
37 }
38 }
39 }
40 return count;
41};
42
This JavaScript solution performs BFS starting from boundary islands using a queue. The queue helps in exploring nodes layer by layer (BFS style), marking reachable nodes. Remaining unmarked are counted as enclaves.