Largest Island After One Land Conversion
Problem statement
You are given an n x n binary matrix grid. You may change at most one 0 into 1.
An island is a four-directionally connected group of 1s. Return the largest island area obtainable after the optional conversion.
Function
largestIsland(grid: int[][]) → intExamples
Example 1
grid = [[1,0],[0,1]]return = 3Changing either zero joins the two diagonal land cells through the converted cell.
Example 2
grid = [[1,1],[1,0]]return = 4Changing the only zero makes the whole grid one island.
Example 3
grid = [[1,1],[1,1]]return = 4The grid is already one island, so no conversion is needed.
Constraints
1 <= n <= 500.n == grid.length == grid[i].length.grid[i][j]is0or1.- Four-directional adjacency defines an island.