Number of Enclaves
Pattern: Grid graphs
Land cells that can walk off the grid are the ones connected to a border land cell. Sink every land region touching the border, then count the land that is left.
The problem
Given a grid of 1 (land) and 0 (sea), return the number of land cells from which you cannot walk off the edge of the grid (moving up, down, left or right on land).
Think first
- Which land cells can escape?
- Where should the search start?
- What is left after removing them?
Hints
Brute force, and why it fails
From every land cell, search for the border: O((R × C)²).
The key insight
Searching inwards from the border once marks every escaping cell, in O(R × C) total.
Dry run
grid [[0, 0, 0, 0], [1, 0, 1, 0], [0, 1, 1, 0], [0, 0, 0, 0]]. The 1 at (1, 0) is on the border: sink it.
Step 1 of 3
The plan
sink every border land cell and everything joined to it
return the number of 1s left
Complexity
- Time
O(R × C)- Space
O(R × C)
Each cell is visited at most once.
Edge cases
- A 1 × 1 grid.
- A grid with no land (or no walls).
- Everything connected.
I've built my logic, show the code
Java
class Solution {
public int numEnclaves(int[][] grid) {
int R = grid.length, C = grid[0].length;
for (int r = 0; r < R; r++) { sink(grid, r, 0); sink(grid, r, C - 1); } // land joined to the border can escape
for (int c = 0; c < C; c++) { sink(grid, 0, c); sink(grid, R - 1, c); }
int count = 0;
for (int[] row : grid) for (int v : row) count += v; // what is left cannot reach the edge
return count;
}
private void sink(int[][] g, int r, int c) {
if (r < 0 || c < 0 || r >= g.length || c >= g[0].length || g[r][c] != 1) return;
g[r][c] = 0;
sink(g, r + 1, c); sink(g, r - 1, c); sink(g, r, c + 1); sink(g, r, c - 1);
}
}C++
class Solution {
public:
int numEnclaves(vector<vector<int>>& grid) {
int R = static_cast<int>(grid.size()), C = static_cast<int>(grid[0].size());
for (int r = 0; r < R; r++) { sink(grid, r, 0); sink(grid, r, C - 1); } // land joined to the border can escape
for (int c = 0; c < C; c++) { sink(grid, 0, c); sink(grid, R - 1, c); }
int count = 0;
for (const vector<int>& row : grid) for (int v : row) count += v; // what is left cannot reach the edge
return count;
}
void sink(vector<vector<int>>& g, int r, int c) {
if (r < 0 || c < 0 || r >= static_cast<int>(g.size()) || c >= static_cast<int>(g[0].size()) || g[r][c] != 1) return;
g[r][c] = 0;
sink(g, r + 1, c); sink(g, r - 1, c); sink(g, r, c + 1); sink(g, r, c - 1);
}
};Python
class Solution:
def numEnclaves(self, grid: list[list[int]]) -> int:
R, C = len(grid), len(grid[0])
st = [(r, c) for r in range(R) for c in range(C) if (r in (0, R - 1) or c in (0, C - 1)) and grid[r][c] == 1]
for r, c in st:
grid[r][c] = 0 # land joined to the border can escape
while st:
r, c = st.pop()
for x, y in ((r + 1, c), (r - 1, c), (r, c + 1), (r, c - 1)):
if 0 <= x < R and 0 <= y < C and grid[x][y] == 1:
grid[x][y] = 0
st.append((x, y))
return sum(map(sum, grid)) # what is left cannot reach the edgeCommon mistakes
- Searching outwards from every cell.
- Forgetting the corners.