284. Making a large island
Given an n x n binary matrix grid, it is allowed to change at most one 0 to 1. A group of connected 1s forms an island, where two 1s are connected if they share one of their sides.
Return the size of the largest island in the grid after applying this operation.
Example 1:
Input: grid = [[1,0],[0,1]]
Output: 3
Explanation: We change any one 0 to 1 and connect two 1s, then we get an island with maximum area = 3.
Example 2:
Input: grid = [[1,1],[1,1]]
Output: 4
Explanation: The largest island already exists with size 4.
Now Your Turn!
Pick the correct output for the given inputInput: grid = [[1,1],[1,0]]
Still unsure what the problem is asking ?
Let’s go through a few more examples, step by step, to make it clearer.
Constraints:
- 1 <= n <= 500
- 0 <= grid[i][j] <= 1