Learn
Practice
Interview
Resources
Newsletter
F
Toggle theme
0
F
0
Toggle menu
Animations
← Back to All Animations
Number of Enclaves
Bookmark
Brute Force
Boundary DFS
BFS
Union Find
Input
Example 1
No Enclaves
Mixed
Border Ring
Custom
grid
=
[[0,0,0,0],[1,0,1,0],[0,1,1,0],[0,0,0,0]]
0
0
0
0
1
0
1
0
0
1
1
0
0
0
0
0
0
0
0
0
1
0
1
0
0
1
1
0
0
0
0
0
enclaves =
0
0
0
0
0
1
0
1
0
0
1
1
0
0
0
0
0
(0, 1)
is already handled → move on
enclaves =
0
0
0
0
0
1
0
1
0
0
1
1
0
0
0
0
0
(1, 0)
is fresh land → flood it
enclaves =
0
0
0
0
0
1
0
1
0
0
1
1
0
0
0
0
0
(1, 0) is on the edge
→ this island escapes
enclaves =
0
0
0
0
0
1
0
1
0
0
1
1
0
0
0
0
0
(1, 2)
is fresh land → flood it
enclaves =
0
0
0
0
0
1
0
1
0
0
1
1
0
0
0
0
0
visit (2, 1)
→ component = 1
enclaves =
3
0
0
0
0
1
0
1
0
0
1
1
0
0
0
0
0
(1, 3)
is already handled → move on
enclaves =
3
0
0
0
0
1
0
1
0
0
1
1
0
0
0
0
0
(2, 2)
is already handled → move on
enclaves =
3
0
0
0
0
1
0
1
0
0
1
1
0
0
0
0
0
(3, 1)
is already handled → move on
enclaves =
3
0
0
0
0
1
0
1
0
0
1
1
0
0
0
0
0
return 3
algo
master
.
io
Step:
Count the enclaves: land cells that cannot walk off the grid
0 / 28
Input
Example 1
No Enclaves
Mixed
Border Ring
Custom
grid
=
[[0,0,0,0],[1,0,1,0],[0,1,1,0],[0,0,0,0]]
0 / 28
algo
master
.
io
Step:
Count the enclaves: land cells that cannot walk off the grid