Making A Large Island

You are given an n x n binary matrix grid. You are allowed to change at most one 0 to be 1.

Return the size of the largest island in grid after applying this operation.

An island is a 4-directionally connected group of 1s.

Example 1
1 0
0 1
Inputgrid = [[1,0],[0,1]]
Output3
Change one 0 to 1 and connect two 1s, then we get an island with area = 3.
Example 2
1 1
1 0
Inputgrid = [[1,1],[1,0]]
Output4
Change the 0 to 1 and make the island bigger, only one island with area = 4.

Constraints

  • n == grid.length
  • n == grid[i].length
  • 1 <= n <= 500
  • grid[i][j] is either 0 or 1.

Asked at 15 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate