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.
1#include <stdio.h>
2
3void dfs(int** grid, int m, int n, int i, int j) {
4 if (i < 0 || j < 0 || i >= m || j >= n || grid[i][j] == 0) {
5 return;
6 }
7 grid[i][j] = 0; // Marking visited land
8 dfs(grid, m, n, i + 1, j);
9 dfs(grid, m, n, i - 1, j);
10 dfs(grid, m, n, i, j + 1);
11 dfs(grid, m, n, i, j - 1);
12}
13
14int numEnclaves(int** grid, int gridSize, int* gridColSize) {
15 int m = gridSize;
16 int n = *gridColSize;
17 for (int i = 0; i < m; i++) {
18 if (grid[i][0] == 1) {
19 dfs(grid, m, n, i, 0);
20 }
21 if (grid[i][n-1] == 1) {
22 dfs(grid, m, n, i, n-1);
23 }
24 }
25 for (int j = 0; j < n; j++) {
26 if (grid[0][j] == 1) {
27 dfs(grid, m, n, 0, j);
28 }
29 if (grid[m-1][j] == 1) {
30 dfs(grid, m, n, m-1, j);
31 }
32 }
33 int count = 0;
34 for (int i = 0; i < m; i++) {
35 for (int j = 0; j < n; j++) {
36 if (grid[i][j] == 1) {
37 count++;
38 }
39 }
40 }
41 return count;
42}
43
This C code defines a function dfs
that marks land cells ('1') as water ('0'), effectively marking them as visited. The main function numEnclaves
iterates over the borders of the grid. For each unvisited land cell on the border, DFS is called to mark connected land. Finally, it counts all unvisited land cells inside the grid 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.
1using System;
2using System.Collections.Generic;
3
4public class Solution {
5 public int NumEnclaves(int[][] grid) {
6 int m = grid.Length;
7 int n = grid[0].Length;
8
9 void Bfs(int startRow, int startCol) {
10 Queue<(int, int)> queue = new Queue<(int, int)>();
11 queue.Enqueue((startRow, startCol));
12 grid[startRow][startCol] = 0;
13 int[][] directions = new int[][] { new int[] {1, 0}, new int[] {-1, 0}, new int[] {0, 1}, new int[] {0, -1} };
14 while (queue.Count > 0) {
15 var (r, c) = queue.Dequeue();
16 foreach (var dir in directions) {
17 int newRow = r + dir[0], newCol = c + dir[1];
18 if (newRow >= 0 && newRow < m && newCol >= 0 && newCol < n && grid[newRow][newCol] == 1) {
19 grid[newRow][newCol] = 0;
20 queue.Enqueue((newRow, newCol));
21 }
22 }
23 }
24 }
25
26 for (int i = 0; i < m; i++) {
27 if (grid[i][0] == 1) Bfs(i, 0);
28 if (grid[i][n - 1] == 1) Bfs(i, n - 1);
29 }
30 for (int j = 0; j < n; j++) {
31 if (grid[0][j] == 1) Bfs(0, j);
32 if (grid[m - 1][j] == 1) Bfs(m - 1, j);
33 }
34
35 int count = 0;
36 for (int i = 0; i < m; i++) {
37 for (int j = 0; j < n; j++) {
38 if (grid[i][j] == 1) count++;
39 }
40 }
41 return count;
42 }
43}
44
This C# solution makes use of a queue for BFS. Starting from boundary land cells, connected lands are explored and marked as zero. Unmarked remaining '1's are counted as enclave cells.