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.
1var numEnclaves = function(grid) {
2 const dfs = (i, j) => {
3 if (i < 0 || j < 0 || i >= grid.length || j >= grid[0].length || grid[i][j] === 0) {
4 return;
5 }
6 grid[i][j] = 0;
7 dfs(i + 1, j);
8 dfs(i - 1, j);
9 dfs(i, j + 1);
10 dfs(i, j - 1);
11 };
12
13 const m = grid.length, n = grid[0].length;
14 for (let i = 0; i < m; i++) {
15 if (grid[i][0] === 1) dfs(i, 0);
16 if (grid[i][n - 1] === 1) dfs(i, n - 1);
17 }
18 for (let j = 0; j < n; j++) {
19 if (grid[0][j] === 1) dfs(0, j);
20 if (grid[m - 1][j] === 1) dfs(m - 1, j);
21 }
22
23 let count = 0;
24 for (let i = 0; i < m; i++) {
25 for (let j = 0; j < n; j++) {
26 if (grid[i][j] === 1) {
27 count++;
28 }
29 }
30 }
31 return count;
32};
33
This JavaScript solution performs DFS to visit and mark land cells from border-connected ones. Remaining unvisited land cells in the grid are counted as enclaves.
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.
1#include <vector>
2#include <queue>
3using namespace std;
4
5void bfs(vector<vector<int>>& grid, int i, int j) {
6 int m = grid.size(), n = grid[0].size();
7 queue<pair<int, int>> q;
8 q.push({i, j});
9 grid[i][j] = 0;
10 vector<vector<int>> directions = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
11 while (!q.empty()) {
12 auto [r, c] = q.front();
13 q.pop();
14 for (auto& d : directions) {
15 int newRow = r + d[0], newCol = c + d[1];
16 if (newRow >= 0 && newRow < m && newCol >= 0 && newCol < n && grid[newRow][newCol] == 1) {
17 grid[newRow][newCol] = 0;
18 q.push({newRow, newCol});
19 }
20 }
21 }
22}
23
24int numEnclaves(vector<vector<int>>& grid) {
25 int m = grid.size(), n = grid[0].size();
26
27 for (int i = 0; i < m; i++) {
28 if (grid[i][0] == 1) bfs(grid, i, 0);
29 if (grid[i][n-1] == 1) bfs(grid, i, n-1);
30 }
31 for (int j = 0; j < n; j++) {
32 if (grid[0][j] == 1) bfs(grid, 0, j);
33 if (grid[m-1][j] == 1) bfs(grid, m-1, j);
34 }
35
36 int count = 0;
37 for (int i = 0; i < m; i++) {
38 for (int j = 0; j < n; j++) {
39 if (grid[i][j] == 1) count++;
40 }
41 }
42 return count;
43}
44
The C++ BFS solution employs a queue for expansion of boundary land cells. BFS is initiated on boundary-connected land cells, marking reachable cells as zero. Remaining unvisited land cells constitute the enclaves.