Largest Island After One Fill
grid is a map of 1 (land) and 0 (water). An island is a group of land cells joined side by side; cells that only touch at a corner are not joined.
You may turn at most one water cell into land. Return the size of the largest island you can end up with.
Example 1
grid [[1,1,0],[0,0,1],[1,0,1]]
returns 5
Filling the top right cell joins the island of 2 on the left with the island of 2 on the right: 2 + 2 + 1.
Example 2
grid [[1,1],[1,1]]
returns 4
There is no water to fill. The island already covers all 4 cells.
Example 3
grid [[0,0],[0,0]]
returns 1
Any filled cell becomes an island of size 1.
Constraints
1 <= len(grid) <= 12and1 <= len(grid[0]) <= 12- every cell is 0 or 1
Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.