Problems/827. Making A Large Island

827. Making A Large Island

HardMatrixGraphDFSHash Map

You are given an n x n binary grid where 1 is land and 0 is water. You are allowed to change at most one water cell (0) to a land cell (1).

Return the size of the largest island in the grid after applying this operation. An island is a 4-directionally connected group of land cells.

Example 1
Input: grid = [ [1, 0], [0, 1] ]
Output: 3
By changing grid[0][1] or grid[1][0] to 1, we connect the two isolated land cells into one island of size 3.
Example 2
Input: grid = [ [1, 1], [1, 0] ]
Output: 4
By changing the only water cell at (1,1) to 1, the entire grid becomes land, forming an island of size 4.
Example 3
Input: grid = [ [1, 1], [1, 1] ]
Output: 4
No water cells are available to flip, so the largest island remains the entire grid of size 4.
Visualizer

Visualizer will appear here