Fill the Enclosed Ponds
Implement fillEnclosed
You are given a grid of water (1) and land (0). A pond is a group of water cells connected through shared sides. Every pond that does not touch the border of the grid is enclosed; fill all enclosed ponds with land and return the grid.
The key idea is to think from the outside in: water connected to the border can never be enclosed, so start from the border water and see what it reaches.
Example 1:
Input: grid = [[1,1,0,1],[0,0,1,0],[0,1,1,0],[1,0,0,1]]
Output: [[1,1,0,1],[0,0,0,0],[0,0,0,0],[1,0,0,1]]
Example 2:
Input: grid = [[1,1],[1,1]]
Output: [[1,1],[1,1]]
Example 3:
Input: grid = [[1,1,1],[1,1,1],[1,1,1]]
Output: [[1,1,1],[1,1,1],[1,1,1]]
+ 13 hidden test cases run on Submit.
Constraints:
- ●
1 ≤ rows, cols ≤ 12; grid[r][c] is 1 (water) or 0 (land) - ●
A pond is a group of water cells connected through shared sides - ●
A pond is enclosed when none of its cells lies on the border of the grid - ●
Turn every enclosed pond into land (0) and return the grid; ponds that touch the border stay as they are
grid =
[[1,1,0,1], [0,0,1,0], [0,1,1,0], [1,0,0,1]]