Count Enclosed Freshwater Regions
Problem statement
A rectangular map contains land cells marked 1 and freshwater cells marked 0. Freshwater cells belong to the same region when connected horizontally or vertically.
Count freshwater regions that are completely enclosed by land. A region is not enclosed when any of its cells touches the outer boundary of the map.
Function
countEnclosedFreshwaterRegions(grid: int[][]) → intExamples
Example 1
grid = [[1,1,1,1,1],[1,0,0,1,1],[1,0,1,0,1],[1,1,1,1,1]]return = 2The left group of three zeros is one enclosed region and the isolated zero is another.
Example 2
grid = [[0,1,1],[0,0,1],[1,1,1]]return = 0The only freshwater region reaches the boundary.
Example 3
grid = [[1,1,1],[1,0,1],[1,1,1]]return = 1The center cell is enclosed on every side.
Constraints
1 <= grid.length, grid[i].length <= 500.- The grid is rectangular and contains only
0and1.
Source note: Interview prompt excerpt preserved as source evidence.